select
イベントは、いくらかのテキストが選択されたときに発生します。
バブリング | あり |
---|---|
キャンセル | 不可 |
インターフェイス | ユーザーインターフェイスから作成された場合は UIEvent 、それ以外ならば Event |
イベントハンドラープロパティ | onselect |
このイベントはすべての言語のすべての要素で利用できる訳ではありません。例えば、 HTML では、 select
イベントはフォームの <input type="text">
および <textarea>
要素からのみ発生します。
例
選択範囲をログ出力
<input value="この要素のテキストの一部を選択してみてください。">
<p id="log"></p>
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
プロパティで設定することもできます。
input.onselect = logSelection;
仕様書
仕様書 | 状態 |
---|---|
UI Events select の定義 |
草案 |
ブラウザーの互換性
BCD tables only load in the browser