Document: selectionchange event
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.
Theselectionchange event of theSelection API is fired when the currentSelection of aDocument is changed.
This event is not cancelable and does not bubble.
The event can be handled by adding an event listener forselectionchange or using theonselectionchange event handler.
Note:This event is not quite the same as theselectionchange events fired when the text selection in an<input> or<textarea> element is changed. See theselectionchange event ofHTMLInputElement for more details.
In this article
Syntax
Use the event name in methods likeaddEventListener(), or set an event handler property.
js
addEventListener("selectionchange", (event) => { })onselectionchange = (event) => { }Event type
A genericEvent.
Examples
js
// addEventListener versiondocument.addEventListener("selectionchange", () => { console.log(document.getSelection());});// onselectionchange versiondocument.onselectionchange = () => { console.log(document.getSelection());};Specifications
| Specification |
|---|
| Selection API> # selectionchange-event> |
| Selection API> # dom-globaleventhandlers-onselectionchange> |