Option index Property

❮ Option Object

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 »

Description

The index property sets or returns the index position of an option in a drop-down list.

The index starts at 0.


Browser Support

Property
index Yes Yes Yes Yes Yes

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
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.