<p>Try highlighting some of the elements in these paragraphs.</p>
<p>
Once you do, you can remove the selected content by clicking the button below.
</p>
<h2>All of the selected text/nodes will be removed.</h2>
<button>Delete selected text</button>Selection: deleteFromDocument() method / notebook
Selection: deleteFromDocument() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
let button = document.querySelector("button");
button.addEventListener("click", deleteSelection);
function deleteSelection() {
let selection = window.getSelection();
selection.deleteFromDocument();
}