Option indexProperty

Option 对象参考手册Option Object

Definition and Usage

The index property returns the index position of an option in the dropdown list.

The index starts at 0.

Syntax

Set the index property:

optionObject.index=integer

Return the index property:

optionObject.index


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the index property.


Example

Example

This example returns the index position of the selected option in the dropdown list:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Rookie Tutorial (example.com)</title>
<script>
function displayResult(){
    var x=document.getElementById("mySelect").selectedIndex;
    var y=document.getElementById("mySelect").options;
alert("Index: " + y[x].index + " is " + y[x].text);
}
</script>
</head>
<body>

<form>
Choose your favorite fruit:
<select id="mySelect">
    <option>Apple</option>
    <option>Orange</option>
    <option>Pineapple</option>
    <option>Banana</option>
</select>
</form>
<button type="button" onclick="displayResult()">Display index</button>

</body>
</html>

Try it yourself »


Option 对象参考手册Option Object Other Extensions