How to programmatically change the src of an img tag with JavaScript?

Spread the love

Sometimes, we want to programmatically change the src of an img tag with JavaScript.

In this article, we’ll look at how to programmatically change the src of an img tag with JavaScript.

How to programmatically change the src of an img tag with JavaScript?

To programmatically change the src of an img tag with JavaScript, we set the src property of the img element.

For instance, we write

document.getElementById("imageid").src = "../template/save.png";

to select the img element with getElementById.

And then we set its src property to the new image path.

Conclusion

To programmatically change the src of an img tag with JavaScript, we set the src property of the img element.

Leave a Reply

Your email address will not be published. Required fields are marked *