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.