Sometimes, we want to set img src with JavaScript.
In this article, we’ll look at how to set img src with JavaScript.
How to set img src with JavaScript?
To set img src with JavaScript, we set the src
property of the img element.
For instance, we write
const image = document.createElement("img");
image.src = "https://picsum.photos/200/300";
to call createElement
to create an img element.
Then we set its src attribute by setting the src
property to the image URL.
Conclusion
To set img src with JavaScript, we set the src
property of the img element.