Highlight text in html with javascript jQuery

I want to highlight all keywords (case insensitive) in the p tag programmatically

if keywords

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

Hi should be highlighted in blue

+3
source share
2 answers

I think you are looking for a jQuery highlight plugin .

Once you download it, 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 selectoris what element in the document you want to find. If you want this done on the whole page, just put it 'body'.

+3
source

Source: https://habr.com/ru/post/1705158/


All Articles