Javascript

Highlight all occurrences of text in a container

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 :

http://jsfiddle.net/akonchady/n68wo4w1/2/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s