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();