how to swap Image in jquery



How can I swap an image’s SRC attribute, on click of that image?

<a href="#">
    <img src="./layout/images/search.png" />

$('img[src="./layout/images/search.png"]').click(function () {





Try this –

$("img").click(function () {

As David Hedlund points out this will change all images on a page due to the img selector. You could target an image using a class as sam152 suggests or, you could target an image using it’s src attribute –

 $("img[src='old.gif']").click(function () {

Demo –


Facebook Comments

Post a comment