0

I have a JSF selectOneMenu and need to select option 2 (out of 3) dynamically with jQuery. I use the $('[id$=tripple_choice]').show(); $('[id$=tripple_choice]').hide(); to show/hide the whole menu.

UPDATE:

The generated HTML is:

<select name="view:custform:tripple_choice" size="1" class="" id="view:custform:tripple_choice">
  <option value="" selected="true">-- Select --</option>
  <option value="opt1">opt1</option>
  <option value="opt2">opt2</option>
</select>

Any ideas?

user1414745
  • 1,317
  • 6
  • 25
  • 45

1 Answers1

1

Something like this, perhaps?

$('[id$=tripple_choice]').eq(1).show();

You can use :eq expression in your selector as well...

$('[id$=tripple_choice]:eq(1)').show();

... but this is actually discouraged by jQuery:

Because :eq() is a jQuery extension and not part of the CSS specification, queries using :eq() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use $("your-pure-css-selector").eq(index) instead.

UPDATE: in your case you need to update the selectedIndex property of HTMLSelectObject. It's done with this line...

$('[id$=tripple_choice]')[0].selectedIndex = 1;
raina77ow
  • 103,633
  • 15
  • 192
  • 229