ユーザが、<select>要素の選択した選択肢を変更したときに JavaScript を実行します:
<select onchange="myFunction()">
Try it Yourself ❯
他の "Try it Yourself" の例が下にあります。
onchange 属性は、要素の値が変更された瞬間に発火します。
チップ: このイベントは、oninput イベントに似ています。 違いは、 oninput イベントが要素の値が変更された直後に発生する一方で、onchange は要素がフォーカスを失ったときに発生します。 その他の違いは、onchange イベントは <keygen> および <select> 要素でも動作することです。.
イベント属性 | |||||
---|---|---|---|---|---|
onchange | Yes | Yes | Yes | Yes | Yes |
なし。
<element onchange="script">
値 | 説明 |
---|---|
script | script は onchange で実行される |
サポートする HTML タグ: | <input type="checkbox">, <input type="file">, <input type="password">, <input type="radio">, <input type="range">, <input type="search">, <input type="text">, <keygen>, <select> and <textarea> |
---|
ユーザが入力フィールドの内容を変更したときに JavaScript を実行します:
<input type="text" name="txt" value="Hello" onchange="myFunction(this.value)">
Try it Yourself ❯
HTML DOM リファレンス:onchange イベント