I've been working on a highlight script. The first result can be found here substring selector with jquery?
The script http://jsfiddle.net/TPg9p/3/
But unfortunately it only works with a simple string. I want it to work with string that contain tags inside.
Example :
<li>sample string li span style="color:red" id
<span id="toto" style="color:red">color id</span>
abcde
</li>
So if the user search for span
it should only match the span inside the <li>
and before the tag span
but not the tag span
itself. Then the matched string is replace with <span class="highlight">span</span>
The same for other attributes or content of an attributes. Anything inside an opening tag and end tag should be ignored.
Since HTML is about DOM and nodes. Could we parse this string into nodes then select only the text node to replace it?
Please answer by updating the jsFiddle above.
UPDATED
Demo of working solution by Tibos : http://jsfiddle.net/TPg9p/10/
` while `color` belong to the node ``. It's true that the original question asked to search for a single word. But it's just an example. Imagine the case the user search for the whole sentence, then it should highlight the whole sentence... So if I search for `id color` then it should match that couple of words and not `id` and `color` seperatedly. Your version works a bit better than raina77ow's because it can match text which belong to different text nodes.
– Thanh Trung Nov 27 '13 at 13:34