HTMLOptionsCollection: selectedIndex-Eigenschaft

Die selectedIndex-Eigenschaft der HTMLOptionsCollection-Schnittstelle ist der numerische Index des ersten ausgewählten <option>-Elements, falls vorhanden, oder -1, wenn kein <option> ausgewählt ist. Das Setzen dieser Eigenschaft wählt die Option an diesem Index aus und hebt die Auswahl aller anderen Optionen in dieser Sammlung auf, während das Setzen auf -1 alle derzeit ausgewählten Elemente deselektiert. Diese Eigenschaft entspricht genau der selectedIndex-Eigenschaft des HTMLSelectElement, dem diese Sammlung gehört.

Wert

Eine Zahl.

Beispiele

js
const optionColl = document.getElementById("select").options;
console.log(`selected option: ${optionColl.selectedIndex}`); // the index of the first selected option, or -1 if no option is selected
optionColl.selectedIndex = 0; // selects the first item
optionColl.selectedIndex = -1; // deselects any selected option

Spezifikationen

Specification
HTML
# the-htmloptionscollection-interface:dom-htmloptionscollection-selectedindex-2

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
selectedIndex

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch