MDN self-hosted library · e01.org
Selection API / notebook

Selection API

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

js
const selection = window.getSelection();
alert(selection); // Shows a string
js
const selectedText = selection.toString();
js
const selObj = window.getSelection();
const range = selObj.getRangeAt(0);