sol
sol

Reputation:

highlight text in html with javascript jQuery

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

Upvotes: 3

Views: 1691

Answers (2)

Marcel St&#246;r
Marcel St&#246;r

Reputation: 23525

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

Upvotes: -1

Paolo Bergantino
Paolo Bergantino

Reputation: 488354

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

Upvotes: 3

Related Questions