3

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 Answers 2

3

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'.

Sign up to request clarification or add additional context in comments.

Comments

-1

For a lenient jQuery highlight plugin you may want to consider http://www.frightanic.com/2011/02/27/lenient-jquery-highlight-plugin-javascript/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.