HTMLInputElement: select イベント
select イベントは、いくらかのテキストが選択されたときに発生します。
構文
このイベント名を addEventListener() 等のメソッドで使用するか、イベントハンドラープロパティを設定するかしてください。
js
addEventListener("select", (event) => {});
onselect = (event) => {};
イベント型
一般的な Event です。
例
選択範囲をログ出力
html
<input value="この要素のテキストの一部を選択してみてください。">
<p id="log"></p>
js
function logSelection(event) {
const log = document.getElementById("log");
const selection = event.target.value.substring(
event.target.selectionStart,
event.target.selectionEnd
);
log.textContent = `You selected: ${selection}`;
}
const input = document.querySelector("input");
input.addEventListener("select", logSelection);
onselect による同等の処理
イベントハンドラーを onselect プロパティで設定することもできます。
js
input.onselect = logSelection;
仕様書
| Specification |
|---|
| HTML Standard # event-select |
| HTML Standard # handler-onselect |
ブラウザーの互換性
BCD tables only load in the browser