Highlight Text in div inner Html

Posted by kgovindarao523-21772 under JavaScript on 7/26/2013 | Points: 10 | Views : 1603 | Status : [Member] [MVP] | Replies : 1
Hi All,

consider the following div is inside body tag.

<div id="test">
this is <a href="#">my</a> text that needs highlighting
</div>

Now to highlight the term ' mytext 'and re assign the inner html to the div, javascript code is :
--------------------------------
var src_str = $("#test").html();
var term = "my text";
term = term.replace(/(\s+)/,"(<[^>]+>)*$1(<[^>]+>)*");
var pattern = new RegExp("("+term+")", "i");
src_str = src_str.replace(pattern, "<mark>$1</mark>");
src_str = src_str.replace(/(<mark>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/,"$1</mark>$2<mark>$4");
$("#test").html(src_str);



This is fine .
--------------------------------
Now my requirement has changed to highlight 'mytext' in following div.

<div id="test">
this is <nobr>my</nobr><nobr>text that needs highlighting</nobr>
</div>

Please provide me solution as early as possible.

Thank you,
Govind



Responses

Posted by: Jeyanthi on: 8/8/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

Can you go through the following links for your reference

http://www.dotnetbull.com/2011/10/highlighting-search-text-inside-div.html
http://www.sitepoint.com/forums/showthread.php?459934-selecting-text-inside-lt-div-gt

Regards,
Jeyanthi

kgovindarao523-21772, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response