--- title: GlobalEventHandlers.oninput slug: Web/API/GlobalEventHandlers/oninput tags: - API - Event Handler - GlobalEventHandlers - HTML DOM - Property - Reference translation_of: Web/API/GlobalEventHandlers/oninput ---
{{domxref("GlobalEventHandlers")}} ミックスインの oninput プロパティは、{{HTMLElement("input")}}、{{HTMLElement("select")}}、{{HTMLElement("textarea")}} の各要素上の {{event("input")}} イベントを処理する {{event("Event_handlers", "event handler")}} です。これは、{{domxref("HTMLElement.contentEditable", "contenteditable")}} または {{domxref("Document.designMode", "designMode")}} が有効になっている要素上のイベントも扱います。
注記: oninput と異なり、{{domxref("GlobalEventHandlers.onchange", "onchange")}} イベントハンドラーは、各要素の value が変化するたびに呼び出される必要がありません。
target.oninput = functionRef;
functionRef は関数名または 関数式 です。この関数は、{{domxref("KeyboardEvent")}} オブジェクトとその 1 個の引数を受け取ります。
この例は、{{HtmlElement("input")}} 要素のコンテンツを変更する度に、その文字数をログ出力します。
<input type="text" placeholder="ここに何か入力すると、その長さが分かります。" size="50"> <p id="log"></p>
let input = document.querySelector('input');
let log = document.getElementById('log');
input.oninput = handleInput;
function handleInput(e) {
log.textContent = `フィールドの value は
${e.target.value.length} 文字の長さです。`;
}
{{EmbedLiveSample("Example")}}
| 仕様書 | 策定状況 | 備考 |
|---|---|---|
| {{SpecName("HTML WHATWG", "#ix-handler-oninput", "oninput")}} | {{Spec2("HTML WHATWG")}} | 初期定義 |
{{Compat("api.GlobalEventHandlers.oninput")}}
以下のリンクは、互換性の問題と修正についての議論です。古いブラウザーで動作させる場合に役立つでしょう: