The Document selectionchange event fires when the document’s current Selection changes—selected text across DOM nodes, or a collapsed caret. Learn to listen on document, read document.getSelection(), and practice with five try-it labs.
01
Kind
Document event
02
Type
Event (generic)
03
Cancelable
No
04
Bubbles
No
05
Read
document.getSelection()
06
Status
Baseline · Widely available
Fundamentals
Introduction
When you drag to highlight text on a page—or click so the caret moves—the browser updates the document Selection. Each time that happens, Document selectionchange fires.
The event object itself does not carry the new selection. Inside the handler, call document.getSelection() (or window.getSelection()) to read the current Selection object.
💡
Beginner tip
Document selectionchange is different from the selectionchange that fires on <input> / <textarea>. Those use character offsets (selectionStart / selectionEnd) and do bubble. Document selection uses DOM ranges and does not bubble.
Concept
Understanding Document selectionchange
A standard Document event that answers: “Did the page’s text selection (or caret) just change?”
Fires when the current Selection of a Document changes (MDN).
Includes creating/clearing a selection, moving range ends, replacing a range, or collapsing to a caret.
Not cancelable and does not bubble — listen on document.
Event type — a generic Event.
Handler — document.onselectionchange or addEventListener("selectionchange", ...).
Status — Baseline Widely available since March 2017 (MDN).
Foundation
📝 Syntax
Use the event name with addEventListener, or set the handler property:
A generic Event. Not cancelable and does not bubble.
MDN-style handlers
JavaScript
// addEventListener version
document.addEventListener("selectionchange", () => {
console.log(document.getSelection());
});
// onselectionchange version
document.onselectionchange = () => {
console.log(document.getSelection());
};
When it fires (MDN)
Situation
Beginner meaning
Create or clear a selection
User highlights text or deselects
Start/end boundary moves
Drag handles / extend selection
Range changes completely
Selection replaced as a whole
Collapsed caret
Selection shrinks to a single caret position
Compare
⚖️ Document vs input selectionchange
Topic
Document
Input / textarea
What selection?
DOM ranges across nodes
Offsets inside the control’s value
How to read
document.getSelection()
selectionStart, selectionEnd, selectionDirection
Bubbles?
No (fires on Document)
Yes (fires on the control)
Handler property
onselectionchange on document
Also onselectionchange on the element
Typical use
Page text highlight tools
Caret / highlight inside a form field
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Listen
document.addEventListener("selectionchange", fn)
Handler property
document.onselectionchange = fn
Current Selection
document.getSelection()
Selected text
String(document.getSelection()) or .toString()
Collapsed caret?
selection.isCollapsed
Range count
selection.rangeCount
MDN status
Baseline Widely available (Mar 2017)
Snapshot
🔍 At a Glance
Four facts to remember about Document selectionchange.
Event type
Event
No selection payload
Means
Selection changed
Highlight or caret
Read with
getSelection()
Call inside the handler
Baseline
yes
Widely available
Hands-On
Examples Gallery
Examples follow MDN Document: selectionchange event. In try-it labs, select text on the page (not only inside an input) to fire the Document event, then inspect document.getSelection().
📚 Getting Started
Log the Selection object with both listener styles.
Example 1 — Log getSelection() (MDN)
Print the Selection whenever the document selection changes.
Selection { … } // or the selected string in some consoles
How It Works
Matches MDN’s basic usage: the listener does not read properties from event—it asks the Document for the current Selection.
Example 2 — document.onselectionchange
Use the handler property and show whether anything is selected.
JavaScript
const out = document.getElementById("out");
document.onselectionchange = () => {
const sel = document.getSelection();
out.textContent = sel && sel.toString()
? "Selection active (" + sel.toString().length + " chars)"
: "No text selected (caret or empty)";
};
isCollapsed: true | rangeCount: 1 | text length: 0
isCollapsed: false | rangeCount: 1 | text length: 18
How It Works
A collapsed selection is usually a caret with no highlighted characters. A non-empty highlight sets isCollapsed to false.
Example 5 — Live Selection Preview
Update a sticky preview box as the user selects page text.
JavaScript
const preview = document.getElementById("preview");
document.addEventListener("selectionchange", () => {
const text = String(document.getSelection() || "").trim();
preview.textContent = text
? "You selected: " + text
: "Select any text on this page…";
});
Document selectionchange is marked Baseline Widely available on MDN (since March 2017). Logos use the shared browser-image-sprite.png sprite from this project. Read the Selection with document.getSelection().
✓ Baseline · Widely available
Document selectionchange
Fires when the Document Selection changes. Inspect the live Selection with document.getSelection().
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium Edge
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLegacy support in older IE; prefer modern browsers
Legacy
selectionchangeExcellent
Bottom line: Listen on document for selectionchange, call getSelection() in the handler, and keep work light because the event fires often while dragging.
Wrap Up
Conclusion
Document selectionchange is the Selection API signal that the page highlight or caret changed. Pair it with document.getSelection() to read the live Selection—the Event itself stays a simple notification.
Confuse Document selection with input selectionStart
Assume the event bubbles from an element
Run heavy work on every drag tick without throttling
Call this Experimental—it is Baseline Widely available
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about selectionchange
Selection changed — read it with document.getSelection().
5
Core concepts
📄01
Selection changed
Highlight or caret updated
Event
🔍02
Read separately
document.getSelection()
API
🔁03
No bubble
Listen on document
DOM
✍️04
Not input API
Different from textarea
Compare
✅05
Baseline ready
Widely available
Status
Next up: visibilitychange — when the page becomes visible or hidden.
Questions
❓ Frequently Asked Questions
What is the Document selectionchange event?
It fires when the current Selection of a Document changes—for example when the user highlights text, clears a selection, moves a range boundary, or collapses to a caret.
Is selectionchange deprecated or experimental?
No. MDN marks Document selectionchange as Baseline Widely available (since March 2017). It is not Deprecated, Experimental, or Non-standard.
Does selectionchange bubble?
No. MDN states the Document selectionchange event is not cancelable and does not bubble. Listen on document.
How do I read the selection?
Call document.getSelection() (or window.getSelection()) inside the handler. The Event object itself does not contain the updated selection details.
How is it different from input selectionchange?
Document selection uses DOM ranges via getSelection(). Input/textarea selection uses selectionStart/selectionEnd and that selectionchange event bubbles from the control.
Is there an onselectionchange property?
Yes. You can use document.onselectionchange or document.addEventListener("selectionchange", ...).
Did you know?
A document selection can represent either a range of selected content across DOM nodes or a collapsed caret position—both still go through selectionchange and getSelection().