Programmatically change the src of an img tag Programmatically change the src of an img tag javascript javascript

Programmatically change the src of an img tag


Give your img tag an id, then you can

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


You can use both jquery and javascript method:if you have two images for example:

<img class="image1" src="image1.jpg" alt="image"><img class="image2" src="image2.jpg" alt="image">

1)Jquery Method->

$(".image2").attr("src","image1.jpg");

2)Javascript Method->

var image = document.getElementsByClassName("image2");image.src = "image1.jpg"

For this type of issue jquery is the simple one to use.


if you use the JQuery library use this instruction:

$("#imageID").attr('src', 'srcImage.jpg');