Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a CSS Stylesheet with Javascript .innerHTML

If I have a javascript function that loads an error using

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

And I want to stylize it say change the text to a different color, and put the error into a different position, how would I go about doing that? I've been looking around, and cannot find any information.

like image 386
Jack Parker Avatar asked Nov 07 '25 05:11

Jack Parker


2 Answers

As innerHTML suggests you can also add HTML. You could for instance add a span tag with a class that you style via CSS.

JS/CSS and HTML

document.getElementById('error-message').innerHTML = "<span class='error'>my error</span>";
.error {
  color: red;
  font-size: 24px;
}
<div id="error-message"></div>
like image 78
Dieterg Avatar answered Nov 08 '25 19:11

Dieterg


Check this out , this answer should suffice to your needs:

document.getElementById("ID").innerHTML = "This is your error";
var sheet = document.createElement('style')
sheet.innerHTML = "div {color:red;overflow:hidden;}";
document.body.appendChild(sheet);

Add the below line to your span

#myspan{float:left;}

Working fiddle:http://jsfiddle.net/0z971bsn/

As you mentioned in the comments , to check for errors you can refer to the answer to this question How to get my forms' Javascript error function to cancel form submission and insert error message?

like image 23
m0bi5 Avatar answered Nov 08 '25 19:11

m0bi5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!