--- title: GlobalEventHandlers.onchange slug: Web/API/GlobalEventHandlers/onchange tags: - API - Event Handler - GlobalEventHandlers - HTML DOM - Property - Reference translation_of: Web/API/GlobalEventHandlers/onchange ---
{{domxref("GlobalEventHandlers")}} ミックスインの onchange プロパティは、change イベントを処理する {{domxref("EventHandler")}} です。
change イベントはユーザーがフォームコントロールの値を変更した時に発生します。これは例えば、コントロールの外側をクリックしたり Tab キーで別のコントロールへ切り替えたりすることにより行われることがあります。
注記: {{domxref("GlobalEventHandlers.oninput", "oninput")}} と異なり、onchange イベントハンドラーは、各要素の value が変化するたびに呼び出される必要がありません。
target.onchange = functionRef;
functionRef は関数名または 関数式 です。この関数は、{{domxref("Event")}} オブジェクトとその 1 個の引数を受け取ります。
この例は、{{HtmlElement("input")}} 要素のコンテンツを変更してフォーカスを要素から外す度に、その文字数をログ出力します。
<input type="text" placeholder="Type something here, then click outside of the field." size="50"> <p id="log"></p>
let input = document.querySelector('input');
let log = document.getElementById('log');
input.onchange = handleChange;
function handleChange(e) {
log.textContent = `The field's value is
${e.target.value.length} character(s) long.`;
}
{{EmbedLiveSample("Example")}}
| 仕様書 | 策定状況 | 備考 |
|---|---|---|
| {{SpecName('HTML WHATWG','webappapis.html#handler-onchange','onchange')}} | {{Spec2('HTML WHATWG')}} |
{{Compat("api.GlobalEventHandlers.onchange")}}