Categories
JavaScript Answers

How to display JavaScript variables in a HTML page without document.write?

Spread the love

Sometimes, we want to display JavaScript variables in a HTML page without document.write.

In this article, we’ll look at how to display JavaScript variables in a HTML page without document.write.

How to display JavaScript variables in a HTML page without document.write?

To display JavaScript variables in a HTML page without document.write, we set the element’s innerHTML property.

For instance, we write

<div class="results"></div>

to add a div.

Then we write

document.querySelector(".results").innerHTML = "Hello World!";

to select the div with querySelector.

And then we set its innerHTML property to the content we want to display in it.

Conclusion

To display JavaScript variables in a HTML page without document.write, we set the element’s innerHTML property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *