Document: selectionchange event
Selection API
Selection API
The browser fires the **`selectionchange`** event of the [Selection API](/docs/Web/API/Selection) when the current Selection of a Document changes. A document selection represents either a range of selected content across DOM nodes or a collapsed caret position.
This event is not cancelable and does not bubble.
## Syntax
Use the event name in addEventListener():
```js-nolint
addEventListener("selectionchange", (event) => {})
```
Alternatively, assign to the [event handler property](/docs/Web/API/Document_Object_Model/Events#using_onevent_properties) of the interface:
```js-nolint
onselectionchange = (event) => {}
```
## Event type
A generic Event.
## Description
The `Document` object `selectionchange` event is fired when:
- A user or script creates or clears a selection.
- The start or end boundary point of a selected range moves.
- A selected range changes completely.
- A selection collapses to a single caret position.
The event object itself does not contain the updated selection details. You can retrieve the current selection by calling document.getSelection() within your event listener.
> [!NOTE]
> When you change the selection programmatically, for example by calling Selection.collapse(), the selection updates immediately, but the `selectionchange` event is queued as a task. Its listeners run later, after the current script finishes executing. This contrasts with events such as `focus` and `click`, whose listeners run synchronously when triggered by focus() and click(), respectively.
This event differs significantly from the `selectionchange` event fired on input and textarea text controls:
- Document selections use DOM node positions and require Document.getSelection() for inspection. Text inputs maintain independent selections within their internal text values, using character offsets inspected via `selectionStart`, `selectionEnd`, and `selectionDirection`.
- The document-level `selectionchange` event fires directly on the Document and does not bubble. The text input `selectionchange` event fires on the input/textarea element and bubbles up the DOM tree.
See the selectionchange event of `HTMLInputElement` and the selectionchange event of `HTMLTextAreaElement` for more details of the text input events.
## Examples
### Basic usage
```js
// addEventListener version
document.addEventListener("selectionchange", () => {
console.log(document.getSelection());
});
// onselectionchange version (but note that this replaces other event listeners
// added using onselectionchange, if any)
document.onselectionchange = () => {
console.log(document.getSelection());
};
```
## Specifications
## Browser compatibility
## See also
- selectstart
- Document.getSelection()
- Selection
Cite this
"Document: selectionchange event". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/Document/selectionchange_event JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy