<select id="sel">
<option value="123" selected="selected">text1</option>
<option value="44">text2</option>
<option value="882">text3</option>
...
</select>
How to get the index of selected option with jQuery?
May be .index(subject)
, but all possibilities tested, didn't work...
P.S. Indexes: value="123" => 0, value="44" => 1, ...
Thanx
This will do it:
You can actually do it without jQuery:
var sel = document.getElementById( 'sel' ); var index = sel.selectedIndex;
The title doesn't quite match the question...
index of selected select->option tag:
You can get the index of the element in this case by checking how many sibling elements the selected element has before it:
or
As I write this, two of the top answers (including the accepted answer) are incorrect despite being pointed out nearly five years ago.
attr("selectedIndex")
does nothing, becauseselectedIndex
is a property on the actual DOM element, not an HTML attribute. You need to useprop
:Interactive demo comparing this to the incorrect version: http://jsfiddle.net/uvwkD/