HTMLSelectElement.selectedIndex
The HTMLSelectElement.selectedIndex
is a long
that reflects the index of the first selected <option>
element. The value -1
indicates that no element is selected.
Syntax
JavaScript
Copy Code
var <var>index</var> = <var>selectElem</var>.selectedIndex; selectElem<code>.selectedIndex = </code>index;
Example
HTML
HTML
Copy Code
<p id="p">selectedIndex: 0</p> <select id="select"> <option selected>Option A</option> <option>Option B</option> <option>Option C</option> <option>Option D</option> <option>Option E</option> </select>
JavaScript
JavaScript
Copy Code
var selectElem = document.getElementById('select') var pElem = document.getElementById('p') // When a new <option> is selected selectElem.addEventListener('change', function() { var index = selectElem.selectedIndex; // Add that data to the <p> pElem.innerHTML = 'selectedIndex: ' + index; })
Specifications
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of 'HTMLSelectElement' in that specification. |
Living Standard | No change since the latest snapshot, HTML5. |
HTML5 The definition of 'HTMLSelectElement' in that specification. |
Recommendation | Initial definition, snapshot of WHATWG HTML Living Standard. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | ? | ? | ? | ? | ? |
Feature | Android | Chrome | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | ? | ? | ? | ? | ? | ? | ? |
License
© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/htmlselectelement/selectedindex