In JavaScript, the browser fires the selectionchange event of the Selection API when the current Selection of a Document changes (MDN). A document selection is either a range of selected content or a collapsed caret.
Register a listener with addEventListener. Check readiness and browser support before you rely on this event.
Syntax
Listen for selectionchange. Start with this line.
JavaScript
addEventListener("selectionchange", …)
Call
What it does
addEventListener("selectionchange", …)
Primary signature.
Built into the browser. Listen with document.addEventListener("selectionchange", …) — no import required.
Library declarations
JavaScript
addEventListener("selectionchange", …)
Parameters
Parameter
Description
Listener
Required. addEventListener or onselectionchange
getSelection()
Required. Current Selection object
Event object
Optional. Generic Event — no selection fields
Return Value
Return Value
Description
*
Fires as a Document event (listener receives the Event object).
JavaScript Examples
Example 1 — Listen and Read getSelection()
MDN pattern: log the Selection whenever the document selection changes.
Listen on document. The event does not bubble, so the target must be document.
Read inside the handler. The Event has no selection payload (MDN).
Call getSelection(). Returns the live Selection object.
Convert to text.toString() gives the highlighted string (empty for a caret).
Example 2 — onselectionchange Property
Same event via the handler property (MDN also documents this form).
JavaScript
document.onselectionchange = () => {
const sel = document.getSelection();
const text = sel ? sel.toString() : "";
console.log(text ? "Selected: " + text : "No text selected");
};
isCollapsed.true means start and end are the same (caret).
rangeCount. Usually 1 when there is a selection object.
length. Zero for a caret; non-zero when text is highlighted.
UI tip. Show a quote/share toolbar only when !isCollapsed and length > 0.
Compatibility
Browser Support
Document selectionchange is part of the Selection API. MDN marks it Baseline Widely available since March 2017 — safe for everyday use in modern browsers.
✓ Baseline Widely available
Document selectionchange
Fires when the Document Selection changes — read state with document.getSelection().
WideBaseline
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
selectionchangeWidely available
Bottom line: Listen on document, then call getSelection() inside the handler. Do not confuse it with the bubbling selectionchange on input/textarea controls.
Frequently Asked Questions
The browser fires selectionchange when the current Selection of a Document changes — create/clear selection, move range boundaries, or collapse to a caret (MDN).
No. MDN marks Document selectionchange as Baseline Widely available (since March 2017). It is not Deprecated, Experimental, or Non-standard.
No. MDN: the Document selectionchange event is not cancelable and does not bubble. Listen on document.
Call document.getSelection() inside the listener. The Event object does not carry the updated selection details (MDN).
Document selection uses DOM ranges via getSelection(). Input/textarea selection uses selectionStart/selectionEnd; that event fires on the control and bubbles (MDN).
Programmatic Selection changes update immediately, but selectionchange is queued as a task and runs after the current script finishes (MDN).