0

My question is very straight forward. I am trying to put style attribute in a html which is in string format. Through a method i am passing html code in string format. And later i am suppose to add a css in that depending on the situation.

I tried:

"<div style='background-color: red'>'Some data'</div>"   // didnt help
"<div style=&quot;background-color: red;&quot;>'Some data'</div>"   // didnt help

In the end:

if(somethingMatches){
   data = data.replace('something', "<mark style='background-color: red;'>" + 'something' + "</mark>")
}

Is there some way that i can pull it off...

TrickOrTreat
  • 821
  • 1
  • 9
  • 23

1 Answers1

0

This is one of the way:

var data = document.body.innerHTML;
data = data.replace(/something/g, "<mark style='background-color: red;'>" + 'something' + "</mark>");
document.body.innerHTML = data;
my name is something<br>
something is going on

If you want to replace all matching words then you should use Regular expression in JavaScript

Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30