Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
189
rated 0 times [  191] [ 2]  / answers: 1 / hits: 19452  / 12 Years ago, fri, august 3, 2012, 12:00:00

i need to highlight all the occurrences of a string in particular div by selecting a string,
once i select a word and click a button it need to highlight all its occurrence inside a div,



eg - if i select




cricket is game




it should highlight all the occurrences of cricket is game some may be like this cricket is game or cricket is game



enter


More From » html

 Answers
30

You can get the browser to do the hard work for you using a TextRange in IE and window.find() in other browsers.



This answer shows how to do it. It will match text that crosses element boundaries and does the highlighting for you using document.execCommand().



Alternatively, James Padolsey recently published a script that I haven't used but looks like it could help: http://james.padolsey.com/javascript/replacing-text-in-the-dom-solved/


[#83880] Thursday, August 2, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
joanneamiyaa

Total Points: 532
Total Questions: 127
Total Answers: 98

Location: Guam
Member since Tue, Nov 3, 2020
4 Years ago
;