Skip to content
Advertisement

How to add style for specific words at HTML using JavaScript?

Could someone help me to write JS function which will take a string(word) as an argument? And then add an additional style (change color for example) for all these words in the HTML. I don’t know where it is located. This “word” can be inside <div>, or <p> or even <b>.

Example:

HTML:

JavaScript

Call function:

JavaScript

Result:

All of the “Hello” words in the HTML have red font color.

By the way, is it possible to write something like this?

Advertisement

Answer

This is the best solution. It works fine at any html, just call matchText("text","black","red");

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