Sometimes, we want to add an img element to a div with JavaScript.
In this article, we’ll look at how to add an img element to a div with JavaScript.
How to add an img element to a div with JavaScript?
To add an img element to a div with JavaScript, we use the appendChild method.
For instance, we write
document.getElementById("placehere").appendChild(elem);
to get the element with ID placehere with getElementById.
Then we call appendChild to add the elem img element as the last child of the element with ID placehere.
Conclusion
To add an img element to a div with JavaScript, we use the appendChild method.