Sometimes, we want to create an img element with JavaScript.
In this article, we’ll look at how to create an img element with JavaScript.
How to create an img element with JavaScript?
To create an img element with JavaScript, we can use the Image constructor.
For instance, we write
const img = new Image(1200, 300);
img.src = "https://picsum.photos/200/300";
to create an Image object with the width and height of the image as arguments.
And then we set its src property to the URL of the image to load.
Conclusion
To create an img element with JavaScript, we can use the Image constructor.