Home > front end >  how to render a JS variable into a html element
how to render a JS variable into a html element

Time:05-06

I am new to Javascript and want to be able to display a JS variable onto my page without the user going into the console as it is neater, and I find a lot of people don't know about the console, and I don't want to use the alert() code. Can anyone help?

CodePudding user response:

The code below accesses the paragraph with id "test" and print the value of "testVariable" into it.

var testVariable = "hello";
document.getElementById("test").innerHTML = testVariable;
<p id="test"></p>

CodePudding user response:

I think this is what you need but this example is very simple i recommend you to google more and to keep open the JavaScript Doc for any questions.

The definition for innerHTML.

The Element property innerHTML gets or sets the HTML or XML markup contained within the element.

// Your variable
let name = "Jhon"

// Get the HTML tag by ID and set the innerHTML
document.getElementById('name').innerHTML = name;
<div >
  <p>Hello <span id="name"></span></p>
</div>

  • Related