Skip to content Skip to sidebar Skip to footer

Can't Get Text Highlighted When Multiple Tags Are Present

So I am trying to highlight text by selecting it and clicking the button (labeled get highlighted text). This is the implementation of it: http://jsbin.com/uzILUro/1 Part of the ht

Solution 1:

You could use window.find() in most browsers, although it may end up being removed from browsers in the future (Mozilla, WebKit). Here's an answer about that:

https://stackoverflow.com/a/5887719/96100

Solution 2:

The main problem is that you're using var searchText = document.getSelection().toString() which doesn't take into account opening and closing tags inside your selection. Instead you should try using searchText = newNode.innerHTML

Make these changes in your getSelectedText function, and you should be golden. Some extra changes might be required for IE, though.

Post a Comment for "Can't Get Text Highlighted When Multiple Tags Are Present"