How can I change the color of inner HTML var value text with JavaScript?

Dear all,

I want to color all the words that I choose in my input from my table rows. the rows table contains a string of paragraphs
my input (word1, word2,…wordn)

var word = 'reference';
var orgTitle = row["Title"];
var org = row["Abstract"];

var result =org.replace(new RegExp(word, "g"),'<span style="color: red;">'+word+'</span>');


I have this script to color a specific words from row table as string TEXT. but ONLY one WORDS as inputs.
Please Need your hepls.
Thank you

Your code example seems to work for me.

Yes it works but to find solution if want to color more then one word as an input for example color refenrence, occurrences,…wordN .

That’s quite straight forward.

Give it a list of words instead:

var words = 'reference serve testing';

Then split by the space, and iterate over each word.

words.split(" ").forEach(function (word) {
  result = result.replace(new RegExp(word, "g"),'<span style="color: red;">'+word+'</span>');

You can see this working at

1 Like

Thank You!! !! i solve it

@Paul_Wilkins i have some problem with this script I have string array of word and I want to use their value as an input to color my paragraph could you help for that this is the LINK : Thank you

I have some problem with this script I have array of words as a string and I want to use their value as an input to color my paragraph could someone help for that this is the LINK
I want my result same as this Link

Thank you for your suggestion.

One of your words to highlight is the two words together “reference serve” which don’t appear together in the abstract, so they don’t get highlighted.

You made the words variable an array, so it doesn’t need to get split. Remove the split part from the code and it works.

// words.split(" ").forEach(...

This my script to Highlight a specific word from text string my only problem is when I want to highlight three sequence words it just the 1st word highlighted then 2nd still without highlight then the third one it is highlighted

The * its a truncation and works well

This my script Example for highlighting n sequence words:

My object is to highlights all the words in the paragraph
looking for your suggestion.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.