September 4, 2012

How to search and highlight the string using jQuery?

Question by Rajasekhar K

How to search the word and highlighting using jquery, can you please help,
here is my code

$("div:icontains('look')").css("background-color", "yellow");

I need to search and highlight the only ‘look’ in the div.

<div>
     On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. 
</div>

Thanks, Rajasekhar.

Answer by Starx

Your selector is wrong, it should be :contains rather than :icontains, with that correction, you code works and here is a demo of that.

Or, you can try using the highlight plugin its quite easy.

A simple example:

$("div:contains('look')").highlight('look').show();

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!