HTMLTextAreaElement: selectionchange event

Baseline 2024
Newly available

Since September 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

The selectionchange event of the Selection API is fired when the text selection within a <textarea> element is changed. This includes both changes in the selected range of characters, or if the caret moves.

This event is not cancelable.

The event is usually processed by adding an event listener on the <textarea>, and in the handler function read by the HTMLTextAreaElement selectionStart, selectionEnd and selectionDirection properties.

It is also possible to add a listener on the global onselectionchange event handler, and within the handler function use Document.getSelection() to get the Selection. However this is not very useful for getting changes to text selections.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("selectionchange", (event) => { })

onselectionchange = (event) => { }

Event type

A generic Event.

Examples

The example below shows how to get the text selected in a <textarea> element.

HTML

html
<div>
  Enter and select text here:<br /><textarea
    id="my-text"
    rows="2"
    cols="20"></textarea>
</div>
<div>selectionStart: <span id="start"></span></div>
<div>selectionEnd: <span id="end"></span></div>
<div>selectionDirection: <span id="direction"></span></div>

JavaScript

js
const myInput = document.getElementById("my-text");

myInput.addEventListener("selectionchange", () => {
  document.getElementById("start").textContent = myInput.selectionStart;
  document.getElementById("end").textContent = myInput.selectionEnd;
  document.getElementById("direction").textContent = myInput.selectionDirection;
});

Result

Specifications

Specification
Selection API
# selectionchange-event
Selection API
# dom-globaleventhandlers-onselectionchange

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
selectionchange event
Chrome – Full support
Chrome 127 (Release date: 2024-07-23)
footnote
footnote Before Chrome 127, a selectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <textarea> elements.
Edge – Full support
Edge 127 (Release date: 2024-07-25)
footnote
footnote Before Edge 127, a selectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <textarea> elements.
Firefox – Full support
Firefox 92 (Release date: 2021-09-07)
footnote Full support
Opera – Full support
Opera 113 (Release date: 2024-08-22)
footnote
footnote Before Opera 113, a selectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <textarea> elements.
Safari – Full support
Safari 18 (Release date: 2024-09-16)
footnote
footnote Before Safari 18, a selectionchange event was fired on Document, see Document's selectionchange event. See bug 271033 for firing the event on <textarea> elements.
Chrome Android – Full support
Chrome Android 127 (Release date: 2024-07-23)
footnote
footnote Before Chrome Android 127, a selectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <textarea> elements.
Firefox for Android – Full support
Firefox for Android 92 (Release date: 2021-09-07)
footnote Full support
Opera Android – Full support
Opera Android 84 (Release date: 2024-08-26)
footnote
footnote Before Opera Android 84, a selectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <textarea> elements.
Safari on iOS – Full support
Safari on iOS 18 (Release date: 2024-09-16)
footnote
footnote Before Safari on iOS 18, a selectionchange event was fired on Document, see Document's selectionchange event. See bug 271033 for firing the event on <textarea> elements.
Samsung Internet – Full support
Samsung Internet 28 (Release date: 2025-04-02)
footnote
footnote Before Samsung Internet 28.0, a selectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <textarea> elements.
WebView Android – Full support
WebView Android 127 (Release date: 2024-07-23)
footnote
footnote Before WebView Android 127, a selectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <textarea> elements.
WebView on iOS – Full support
WebView on iOS 18 (Release date: 2024-09-16)
footnote
footnote Before WebView on iOS 18, a selectionchange event was fired on Document, see Document's selectionchange event. See bug 271033 for firing the event on <textarea> elements.

Legend

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

Full support
Full support
See implementation notes.