Categories
JavaScript Answers

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

Spread the love

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.

Then we set its src property to the path string or URL of the image.

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 *