Skip to content
Advertisement

Highlight a word with jQuery

I basically need to highlight a particular word in a block of text. For example, pretend I wanted to highlight the word “dolor” in this text:

JavaScript

How do I convert the above to something like this:

JavaScript

Is this possible with jQuery?

Edit: As Sebastian pointed out, this is quite possible without jQuery – but I was hoping there might be a special method of jQuery which would let you do selectors on the text itself. I’m already using jQuery heavily on this site, so keeping everything wrapped up in jQuery would make things perhaps a bit more tidy.

Advertisement

Answer

Try highlight: JavaScript text highlighting jQuery plugin. Warning: The source code available on this page contains a cryptocurrency mining script, either use the code below or remove the mining script from the script downloaded from the website.

JavaScript

Also try the “updated” version of the original script.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement