1. 開発者向けのウェブ技術
  2. Web API
  3. HTMLSelectElement
  4. selectedIndex

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

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 ⁨2015年7月⁩.

HTMLSelectElement.selectedIndexlong 値であり、選択されている <option> 要素のうち、multiple の値に応じて最初または最後のものを返します。-1 の値は選択されている要素がないことを示します。

数値です。

HTML

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

js
const selectElem = document.getElementById("select");
const pElem = document.getElementById("p");
// When a new <option> is selected
selectElem.addEventListener("change", () => {
 const index = selectElem.selectedIndex;
 // Add that data to the <p>
 pElem.textContent = `selectedIndex: ${index}`;
});

結果

仕様書

Specification
HTML
# dom-select-selectedindex-dev

ブラウザーの互換性

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.

AltStyle によって変換されたページ (->オリジナル) /