Select selectedIndex property

Definition and Usage

selectedIndex Property 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 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";

Try it yourself

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);

Try it yourself

Example 3

Deselect all options:

document.getElementById("mySelect").selectedIndex = "-1";

Try it yourself

Example 4

If no option is selected, the selectedIndex property will return "-1":

var x = document.getElementById("mySelect").selectedIndex;

Try it yourself

Syntax

Return the selectedIndex property:

selectObject.selectedIndex

Set the selectedIndex property:

selectObject.selectedIndex = number

Attributswert

Wert Beschreibung
number Definiert den Index (Index) der ausgewählten Option in der Dropdown-Liste.

Technische Details

Rückgabewert:

Zahl, die den Index der ausgewählten Option in der Dropdown-Liste darstellt. Der Index beginnt bei 0.

Wird keine Option ausgewählt, beträgt der Rückgabewert -1.

Browser-Unterstützung

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Unterstützung Unterstützung Unterstützung Unterstützung Unterstützung