Select selectedIndexProperty

Select 对象参考手册Select Object

Definition and Usage

The selectedIndex property sets or returns the index number of the selected option in the drop-down list.

Note:If multiple selection is allowed, only the index number of the first selected option is returned.

Syntax

Set the selectedIndex property:

selectObject.selectedIndex=integer

Return the selectedIndex property:

selectObject.selectedIndex


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the selectedIndex property.


Example

Example

The following example alerts the index number of the selected option:

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

</body>
</html>

Try it Yourself »


Select 对象参考手册Select Object Other Extensions