How would i dynamically seta JS variable into say an <h1> tag? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How would i dynamically seta JS variable into say an <h1> tag?

21st Jul 2017, 12:01 AM
James Barrow
James Barrow - avatar
6 Answers
+ 4
Alternatively to writing a new element, using the sample you have in the HTML tab <h1 id= "x"></h1> any of these three sets locate and dynamically set the element's inner HTML. I'm using onload() because otherwise the [JS] tab would load before the document element is ready. window.onload = function() { var el = document.querySelector("#x"); el.innerHTML = x; el = document.getElementsByTagName("h1")[0]; el.innerHTML = x; el = document.getElementById("x"); el.innerHTML = x; }
21st Jul 2017, 12:49 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
Use the + operator to concatenate the values: var x = 100; document.write("<h1>" + x + "</h1>");
21st Jul 2017, 12:28 AM
Alex
0
For what? u can use JavaScript
21st Jul 2017, 12:04 AM
Mr. Nobody
Mr. Nobody - avatar
21st Jul 2017, 12:22 AM
James Barrow
James Barrow - avatar
0
maybe this will help? explain it better
21st Jul 2017, 12:22 AM
James Barrow
James Barrow - avatar
0
thanks man
21st Jul 2017, 12:30 AM
James Barrow
James Barrow - avatar