GeekedOut
GeekedOut

Reputation: 17185

How to set value inside div in JS

I have some code that looks like this:

document.getElementById("error").style.display = 'block';

and when this happens I also want to display the error that is supposed to be shown which is stored in another JS variable. How can I add the value of that variable to the div which has id="error"

Thanks!

Upvotes: 21

Views: 74398

Answers (6)

kinakuta
kinakuta

Reputation: 9037

var displayDiv = document.getElementById('error');
displayDiv.style.display = 'block';
displayDiv.innerHTML = yourVariable;

Upvotes: 3

Shawn
Shawn

Reputation: 1891

document.getElementById('error').innerHTML = variable

(using jQuery would be easier.

Upvotes: 2

epascarello
epascarello

Reputation: 207521

var errorDiv = document.getElementById("error");
errorDiv.innerHTML = yourVariable;
errorDiv.style.display = 'block';

Upvotes: 2

Milan Aleksić
Milan Aleksić

Reputation: 1425

document.getElementById("error").innerHTML = my_js_variable;

Upvotes: 4

patapizza
patapizza

Reputation: 2398

document.getElementById("error").innerHTML = errMsg;

Upvotes: 5

Susam Pal
Susam Pal

Reputation: 34224

var errorMsg = "<p>Example error message</p>"
document.getElementById("error").innerHTML = errorMsg

Upvotes: 35

Related Questions