HTMLSelectElement.selectedIndex
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
HTMLSelectElement.selectedIndex
est un long
qui représente l'index du premier élément sélectionné <option>
. La valeur -1
indique qu'aucun élément est sélectionné.
Syntaxe
js
var index = selectElem.selectedIndex;
selectElem.selectedIndex = index;
Exemple
HTML
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>
JavaScript
js
var selectElem = document.getElementById("select");
var pElem = document.getElementById("p");
// Quand une nouvelle <option> est selectionnée
selectElem.addEventListener("change", function () {
var index = selectElem.selectedIndex;
// Rapporter cette donnée au <p>
pElem.innerHTML = "selectedIndex: " + index;
});
Spécifications
Specification |
---|
HTML # dom-select-selectedindex-dev |
Compatibilité des navigateurs
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
selectedIndex |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.