files/en-us/web/api/document/selectionchange_event/index.md
{{APIRef("Selection API")}}
The selectionchange event of the Selection API is fired when the current {{domxref("Selection")}} of a {{domxref("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
selectionchangeevents fired when the text selection in an {{HTMLElement("input")}} or {{HTMLElement("textarea")}} element is changed. See the {{domxref("HTMLInputElement.selectionchange_event", "selectionchange")}} event ofHTMLInputElementfor more details.
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
addEventListener("selectionchange", (event) => { })
onselectionchange = (event) => { }
A generic {{domxref("Event")}}.
// addEventListener version
document.addEventListener("selectionchange", () => {
console.log(document.getSelection());
});
// onselectionchange version
document.onselectionchange = () => {
console.log(document.getSelection());
};
{{Specifications}}
{{Compat}}