Categories
JavaScript Answers

How to set img src with JavaScript?

Spread the love

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.

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 *