-1

Based off this thread: Using a CSS Stylesheet with Javascript .innerHTML

To stylize a innerHTML such as this:

document.getElementById("ID").innerHTML = "This is your error";

all you need to is this:

document.getElementById('error-message').innerHTML = "<span class='error'>my 
error</span>";

But what If I have a value? How would I apply the tag to this:

document.getElementById('pText').innerHTML = "You clicked: " + value;
j08691
  • 204,283
  • 31
  • 260
  • 272
Enkrypton
  • 39
  • 1
  • 2
  • 15

2 Answers2

1

innerHTML is a string that's converted to / interpreted as HTML, so as long as you add the tags around your value, it'll work:

document.getElementById('pText').innerHTML = "<span>You clicked: " + value + "</span>";

Alternatively, you could add the span to your HTML directly, and set the innerHTML of the span to be "You clicked :" + value in your JS, which is a bit cleaner.

Bricky
  • 2,572
  • 14
  • 30
0

As InnerHtml is a string here i append the value which is got from the click event it works.

function pClicked(value){
 document.getElementById('pText').innerHTML = "<span class='error'>You Clicked "+value+"</span>";
}
.error {
  color: red;
  font-size: 24px;
}
<div id="pClicked" onClick="pClicked('pClicked Div')">Click here</div>

<div id="pText"></div>
Jino Shaji
  • 1,097
  • 14
  • 27