Skip to main content

Command Palette

Search for a command to run...

Highlight all occurrences of text in a container

Published
1 min read
A

As a Senior Principal Engineer at CVSHealth, I specialize in building and optimizing cvs.com to deliver a superior guest experience. Using the latest web technologies, my team and I work diligently to create a user-friendly and intuitive platform that exceeds expectations.

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:

More from this blog

Adarsh Konchady's Blog

23 posts