Javascript selectedIndex Property
Javascript selectedIndex Property Definition and Usage
The selectedIndex property sets or returns the index of the selected option in a drop-down list.
Note: If the dropdown list allows for multiple selections it will only return the index of the first option selected.
Javascript selectedIndex Property Syntax
selectObject.selectedIndex= number
Javascript selectedIndex Property Example
<html>
<head>
<script type="text/javascript">
function getOptionIndex()
{
var x=document.getElementById("mySelect") alert(x.selectedIndex)
}
</script>
</head>
<body>
<form>
Select your favorite fruit:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
<br />
<br />
<input type="button" onclick="getOptionIndex()" value="Alert index of selected fruit">
</form>
</body>
</html>
|