Option index Property

Definition and Usage

index Sets or returns the index position of the option in the dropdown list.

Indexes (indices) start from 0.

See also:

HTML Reference Manual:HTML <option> Tag

Instance

Example 1

Display the index and text of the selected option in the dropdown 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

Example 2

Display the text and index of all options in the dropdown 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

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: A number representing 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