console.log(window.getSelection().containsNode(document.body, true));Selection: containsNode() method / notebook
Selection: containsNode() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
<p>Can you find the secret word?</p>
<p>Hmm, where <span id="secret">SECRET</span> could it be?</p>
<p id="win" hidden>You found it!</p>#secret {
color: transparent;
}const secret = document.getElementById("secret");
const win = document.getElementById("win");
// Listen for selection changes
document.addEventListener("selectionchange", () => {
const selection = window.getSelection();
const found = selection.containsNode(secret);
win.toggleAttribute("hidden", !found);
});