This is my page source :
<body>
<div>
<p><span id="1">word</span> <span id="2">word</span> </p>
<div><span id="3">word</span> <span id="4">word</span></div>
<ul>
<li><span id="5">word</span> <span id="6">word</span></li>
</ul>
</ul>
<p><span id="7">word</span> <span id="8">word</span> <strong><span id="9">word</span></strong> </p>
</div>
</body>
I want to highlight(apply a new class) user selected spans and get it id’s.
I can get user selected content through window.getSelection().But i don’t know how to get selected text nodes.
Thanks in advance,
Logan
The
window.getSelection()returns aselectionobject (ref) that includes the start (anchorNode) and end (extentNode). So based on the HTML you provided – with the ID’s modified to not using only numbers (ref) – here is a demo. Click on any word, or select a group of words to see them get the “red” class name.Modified HTML
Script