Reputation: 4342
I am trying to select an option in a select menu that contains an optgroup. I have tried
document.getElementsByName("dob_year")[0].selectedIndex = "1980";
and it seems to work with select menus that contain no "optgroups", bit it does work with them. How can I select the "1980" option?
<select name="dob_year">
<optgroup label="Morning">
<option value="1980">80</option>
<option value="1981">81</option>
<option value="1982">82</option>
<option value="1983">83</option>
</optgroup>
</select>
Upvotes: 0
Views: 109
Reputation: 60767
As the name suggest, the selectedIndex is an index.
So you have to use the following:
document.querySelector('select').selectedIndex = 2;
Working jsfiddle: http://jsfiddle.net/vkSKy/
Upvotes: 2