Posted on 2009-12-17
I have a search results page where I would like to highlight the search term where it appears within the page.
I have placed the search results within a div tag with id so can reference this, do a regexp and it works fine but I need to exclude all html/xml tags from the function. Can anyone help with this.
So far I have the following which works but if the search term was 'able' this would trash all table tags!
Note: The search term is being generated dynamically so I have entered the static string of Able as a demonstration.
var str = document.getElementById('srchresult').innerHTML;
document.getElementById('srchresult').innerHTML = str.replace(/Able/gi, "<strong style='color:#CC0000'>Able</strong>");