HTMLTextAreaElement: selectionEnd プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

selectionEndHTMLTextAreaElement インターフェイスのプロパティで、 <textarea> 要素内における現在のテキスト選択範囲の末尾の位置を指定します。これは、選択されたテキストの最後のインデックスを表す数値です。 <textarea> の選択されたテキストの末尾のインデックスを取得したり、設定したりする際に使用することができます。

何も選択されていない場合、 selectionStart および selectionEnd の値は、どちらも <textarea> 要素内のカーソル(キャレット)の位置になります。

selectionEnd を現在の selectionStart の値より小さい値に設定すると、selectionEndselectionStart の両方のプロパティがその値に更新されます。 どちらの値も 0 未満の場合、両方のプロパティが textLength プロパティの値に設定されます。

このプロパティ値は、この <textarea> にフォーカスがなくても取得したり設定したりすることができますが、要素にフォーカスがないと、 ::selection 擬似要素が選択テキストと一致しません。

selectionEnd を新しい値に設定すると、 selectchange および select イベントが発生します。

非負の数値です。

js
const textarea = document.getElementById("text-box");
const end = textarea.selectionEnd;

仕様書

Specification
HTML
# dom-textarea/input-selectionend

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
selectionEnd

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報