Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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


1 Answers

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

like image 134
Paolo Bergantino Avatar answered Dec 23 '25 08:12

Paolo Bergantino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!