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 an <input> 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 <input>, and in the handler function read by the HTMLInputElement selectionStart, selectionEnd and selectionDirection properties.
It is also possible to add a listener on the 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.
Use the event name in methods like addEventListener(), or set an event handler property.
addEventListener("selectionchange", (event) => { })
onselectionchange = (event) => { }
A generic Event.
The example below shows how to get the text selected in an <input> element.
<div> Enter and select text here:<br /><input id="my-text" rows="2" cols="20" /> </div> <div>selectionStart: <span id="start"></span></div> <div>selectionEnd: <span id="end"></span></div> <div>selectionDirection: <span id="direction"></span></div>
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;
});
| Specification |
|---|
| Selection API> # selectionchange-event> |
| Selection API> # dom-globaleventhandlers-onselectionchange> |
| 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 |
127Before Chrome 127, aselectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <input> elements. |
127Before Edge 127, aselectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <input> elements. |
92 | 113Before Opera 113, aselectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <input> elements. |
18Before Safari 18, aselectionchange event was fired on Document, see Document's selectionchange event. See bug 271033 for firing the event on <input> elements. |
127Before Chrome Android 127, aselectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <input> elements. |
92 | 84Before Opera Android 84, aselectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <input> elements. |
18Before Safari on iOS 18, aselectionchange event was fired on Document, see Document's selectionchange event. See bug 271033 for firing the event on <input> elements. |
28.0Before Samsung Internet 28.0, aselectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <input> elements. |
127Before WebView Android 127, aselectionchange event was fired on Document, see Document's selectionchange event. See bug 40840956 for firing the event on <input> elements. |
18Before WebView on iOS 18, aselectionchange event was fired on Document, see Document's selectionchange event. See bug 271033 for firing the event on <input> elements. |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/selectionchange_event