X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

Option index Property

Option Object Reference 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 »


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

Internet Explorer Firefox Opera Google Chrome Safari

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;
}
alert(txt);

Try it yourself »


Option Object Reference Option Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook