Easy Tutorial
❮ Prop Style Captionside Prop Datetime Autofocus ❯

Option index Property


Definition and Usage

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

The index starts at 0.

Syntax

Set the index property:

Return the index property:


Browser Support

All major browsers support the index property.


Example

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

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</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>
Select 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()">Show Index</button>

</body>
</html>

❮ Prop Style Captionside Prop Datetime Autofocus ❯