The HTMLSelectElement.selectedIndex
property is a long
that reflects the index of the first or last selected <option>
element, depending on the value of multiple
. The value -1
indicates that no element is selected.
The HTMLSelectElement.selectedIndex
property is a long
that reflects the index of the first or last selected <option>
element, depending on the value of multiple
. The value -1
indicates that no element is selected.
A number.
html
<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>
js
const selectElem = document.getElementById("select"); const pElem = document.getElementById("p"); // When a new <option> is selected selectElem.addEventListener("change", () => { const index = selectElem.selectedIndex; // Add that data to the <p> pElem.textContent = `selectedIndex: ${index}`; });
Specification |
---|
HTML Standard # dom-select-selectedindex-dev |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
selectedIndex |
1 | 12 | 1 | 5.5 | ≤12.1 | 3 | 4.4 | 18 | 4 | ≤12.1 | 1 | 1.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedIndex