Categories
JavaScript Answers

How to add content to HTML body using JavaScript?

Spread the love

Sometimes, we want to add content to HTML body using JavaScript.

In this article, we’ll look at how to add content to HTML body using JavaScript.

How to add content to HTML body using JavaScript?

To add content to HTML body using JavaScript, we call the insertAdjacentHTML method.

For instance, we write

document
  .getElementById("container")
  .insertAdjacentHTML("beforeend", '<div id="idChild"> content html </div>');

to select the element with getElementByid.

Then we call insertAdjacentHTML to insert HTML as the last child of the element with ID container.

Conclusion

To add content to HTML body using JavaScript, we call the insertAdjacentHTML method.

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 *