timkl timkl - 3 months ago 6x
jQuery Question

Toggle src-attribute on image

Is there a way to toggle the src attribute of an image between two values when an anchor is clicked?

Like this:

$('a#some-anchor').click(function() {
// code here that toggles between two image src's e.g. "images/some-image1.jpg" and "images/some-image2.jpg"

$("img").bind("click", function() {
  var src = ($(this).attr("src") === "img1_on.jpg")
                ? "img2_on.jpg" 
                : "img1_on.jpg";
  $(this).attr("src", src);

Taken from here:

Changing the image source using jQuery