Adarsh Konchady
Adarsh Konchady's Blog

Follow

Adarsh Konchady's Blog

Follow

Highlight all occurrences of text in a container

Adarsh Konchady's photo
Adarsh Konchady
·Dec 10, 2015·

1 min read

Here is the JQuery code to highlight all occurrences of a given text in a container:

$(".container:contains('Google')").html(function(_, html) {
  return html.replace(/(Google)/g, '<span style="background-color: yellow;">$1</span>');
});

The above block will change all occurences of the text ‘Methods’ within an element of class ‘container’ and make the background color of the text yellow by inserting a new span.

Check it out here:

 
Share this