Option index eigenschap

Definitie en gebruik

index Eigenschap instellen of retourneren van de indexpositie van de optie in de keuzelijst.

Index (index) begint bij 0.

Zie ook:

HTML Referentiemanual:HTML <option> tag

Voorbeeld

Voorbeeld 1

Toon de index en tekst van de geselecteerde optie in de keuzelijst:

var x = document.getElementById("mySelect").selectedIndex;
var y = document.getElementById("mySelect").options;
alert("Index: " + y[x].index + " is " + y[x].text);

Probeer het zelf

Voorbeeld 2

Toon de tekst en index van alle opties in de keuzelijst:

var x = document.getElementById("mySelect");
var txt = "Alle opties: ";
var i;
for (i = 0; i < x.length; i++) {
    txt = txt + "\n" + x.options[i].text + " heeft index: " + x.options[i].index;
}

Probeer het zelf

Syntax

Return the index property:

optionObject.index

Set the index property:

optionObject.index = integer

Attribute value

Value Description
integer Specifies the index position of the option in the dropdown list.

Technical details

Return value: Number, indicating the index position of the option in the dropdown list. The index starts from 0.

Browser support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support