HTMLSelectElement.selectedIndex
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
HTMLSelectElement.selectedIndex
показывает порядковый номер первого выбранного элемента
. Значение -1 означает, что ни один из элементов не выбран.<option>
Синтаксис
var index = selectElem.selectedIndex; selectElem.selectedIndex = index;
Пример
HTML
html
<p id="p">Индекс выбранного элемента: 0</p>
<select id="select">
<option selected>Option A</option>
<option>Option B</option>
<option>Option C</option>
<option>Option D</option>
<option>Option E</option>
</select>
JavaScript
js
var selectElem = document.getElementById("select");
var pElem = document.getElementById("p");
// Когда выбран новый элемент <option>
selectElem.addEventListener("change", function () {
var index = selectElem.selectedIndex;
// Добавить данные в <p>
pElem.innerHTML = "selectedIndex: " + index;
});
Спецификации
Specification |
---|
HTML # dom-select-selectedindex-dev |
Совместимость с браузерами
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
selectedIndex |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.