Select selectedIndex property
Definition and Usage
selectedIndex
Property to set or return the index (subscript) of the selected option in the dropdown list.
The index starts from 0.
Note:If the dropdown list allows multiple selections, it will only return the index of the first selected option.
Note:The value "-1" will deselect all options (if any).
Note:If no option is selected, the selectedIndex property will return -1.
See also:
HTML Reference Manual:HTML <select> Tag
Example
Example 1
Select the <option> element with index "2":
document.getElementById("mySelect").selectedIndex = "2";
Example 2
Display the index and text of the selected option in the dropdown list:
var x = document.getElementById("mySelect").selectedIndex; var y = document.getElementById("mySelect").options; alert("Index: " + y[x].index + " is " + y[x].text);
Example 3
Deselect all options:
document.getElementById("mySelect").selectedIndex = "-1";
Example 4
If no option is selected, the selectedIndex property will return "-1":
var x = document.getElementById("mySelect").selectedIndex;
Syntax
Return the selectedIndex property:
selectObject.selectedIndex
Set the selectedIndex property:
selectObject.selectedIndex = number
Attribute Value
Value | Description |
---|---|
number | Specifies the index (subscript) of the selected option in the drop-down list. |
Technical Details
Return Value: |
A number representing the index of the selected option in the drop-down list. The index starts from 0. If no option is selected, the return value is -1. |
---|
Browser Support
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support |