Select selectedIndex attribute
Definition and Usage
selectedIndex
Attribute that sets or returns 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 attribute 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 attribute will return "-1":
var x = document.getElementById("mySelect").selectedIndex;
Syntax
Return the selectedIndex attribute:
selectObject.selectedIndex
Set the selectedIndex attribute:
selectObject.selectedIndex = number
Halaga ng Atributo
Halaga | Paglalarawan |
---|---|
number | Tinutukoy ang indeks ng pinili na opsyon sa dropdown list (subscript). |
Detalye ng Teknolohiya
Ibalik na Halaga: |
Bilang, nangangahulugan ng indeks ng pinili na opsyon sa dropdown list. Ang indeks ay nagsisimula sa 0. Kung hindi napili ang anumang opsyon, ang ibabalik na halaga ay -1. |
---|
Suporta ng Browser
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Suporta | Suporta | Suporta | Suporta | Suporta |