views:

408

answers:

1

I want to highlight all the keywords (case insensitive) within a p tag programatically

if the keywords are

var keywords = "hello,thanks, goodbye" // this should be an array
<p>hello world</p>

hello should be highlighted in blue

+2  A: 

I think you are looking for the jQuery highlight plugin.

Once you have it loaded, you can just do something like this:

var words = "hello,thanks, goodbye";
var keywords = words.split(',');
for(var x = 0; x < keywords.length; x++) {
    $(selector).highlight($.trim(keywords[x]));
}

Where selector is what element in the document you want to look for. If you want it to be done to the entire page, just put 'body'.

Paolo Bergantino