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.

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 *