views:

7

answers:

1

I have a page where I am displaying some text in a div and I need to highlight this text in certain parts. I have done this by surrounding the text I need to highlight with a tag and appropriate css styling. E.g. <div> My text will look like this with <span class="highlight">highlighted bits</span> in it. </div>

This works fine. However, another requirement for this page is that the user must be able to select texts, click a button, and the selected text must be highlighted too.

The problem I have is when trying to identify the range of the selected text to grab (using window.getSelection.getRangeAt(0)), this gives me the range which resets after every <span> tag in the text, not from the beginning of the text.