Latest web development tutorials

HTML DOM Select selectedIndex property

Select Object Reference Select Objects

Definition and Usage

selectedIndex property sets or returns the index number of drop-down list of the selected options.

Note: If multiple selections are allowed, then only returns the first index number of the selected option.

grammar

Setting selectedIndex properties:

selectObject.selectedIndex=integer

Back selectedIndex property:

selectObject.selectedIndex


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support selectedIndex property


Examples

Examples

The following examples suggest the index number of the selected option:

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

<form>
选择你最喜欢的水果:
<select id="mySelect">
    <option>Apple</option>
    <option>Orange</option>
    <option>Pineapple</option>
    <option>Banana</option>
</select>
</form>
<button type="button" onclick="displayResult()">显示索引</button>

</body>
</html>

try it"


Select Object Reference Select Objects