W3cubDocs

/Web APIs

Document: selectionchange event

The selectionchange event of the Selection API is fired when the current Selection of a Document is changed.

This event is not cancelable and does not bubble.

The event can be handled by adding an event listener for selectionchange or using the onselectionchange event handler.

Note: This event is not quite the same as the selectionchange events fired when the text selection in an <input> or <textarea> element is changed. See HTMLInputElement.selectionchange_event for more details.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js

addEventListener("selectionchange", (event) => {});

onselectionchange = (event) => {};

Event type

A generic Event.

Examples

js

// addEventListener version
document.addEventListener("selectionchange", () => {
  console.log(document.getSelection());
});

// onselectionchange version
document.onselectionchange = () => {
  console.log(document.getSelection());
};

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
selectionchange_event 11 12 52 5.5 15 5.1 ≤37 18 52 14 5 1.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Document/selectionchange_event