// 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());
};Document: selectionchange event / notebook
Document: selectionchange event
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.