HTMLSelectElement.selectedIndex
HTMLSelectElement.selectedIndex
は long
値であり、選択されている <option>
要素のうち、multiple
の値に応じて最初または最後のものを返します。-1
の値は選択されている要素がないことを示します。
構文
var index = selectElem.selectedIndex;
selectElem.selectedIndex = index;
例
HTML
<p id="p">selectedIndex: 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
var selectElem = document.getElementById('select')
var pElem = document.getElementById('p')
// When a new <option> is selected
selectElem.addEventListener('change', function() {
var index = selectElem.selectedIndex;
// Add that data to the <p>
pElem.innerHTML = 'selectedIndex: ' + index;
})
仕様書
Specification |
---|
HTML Standard # dom-select-selectedindex-dev |
ブラウザーの互換性
BCD tables only load in the browser