Categories
JavaScript Answers

How to add an img element to a div with JavaScript?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.