Option index Property
Example
Display the index and text of the selected option in a drop-down list:
var x = document.getElementById("mySelect").selectedIndex;
var y = document.getElementById("mySelect").options;
alert("Index: " + y[x].index + " is " + y[x].text);
Try it yourself »
Definition and Usage
The index property sets or returns the index position of an option in a drop-down list.
The index starts at 0.
Browser Support
The index property is supported in all major browsers.
Syntax
Return the index property:
optionObject.index
Set the index property:
optionObject.index=integer
Property Values
Value | Description |
---|---|
integer | Specifies the index position of the option within a drop-down list |
Technical Details
Return Value: | A Number, representing the index position of the option within a drop-down list. The index starts at 0. |
---|
More Examples
Example
Display the text and index of all options in a drop-down list:
var x = document.getElementById("mySelect");
var txt = "All options: ";
var i;
for (i = 0; i < x.length; i++) {
txt = txt + "\n"
+ x.options[i].text + " has index: " + x.options[i].index;
}
Try it yourself »
Option Object