jQuery toggle to change image

I have a list of divs with images in them:

<div class="wizard-img"><%= image_tag("sources/pix/nyt.png") %></div>
<div class="wizard-img"><%= image_tag("sources/pix/theguardian.png") %></div>

When a user clicks the div, I'd like it to change the image to x-on.png, when clicked again, it'll change to x-off.png, and when clicked a third time it should revert to x.png.

Is it possible to do this without manually specifying jQuery for every image change? Can I traverse the div and find the image name, and simply append the -off/-on to the image?

Thanks!

Answers


Perhaps CSS sprites would work for you?

That would save you from loading a separate image every time you click the image (button?), and you could solve your problem by adding and removing a css-class, e.g.:

$('.wizard-img').click(
    function() {
        if ($(this).hasClass('on')) {
            $(this).removeClass('on').addClass('off');
        } else if ($(this).hasClass('off')) {
            $(this).removeClass('off');
        } else {
            $(this).addClass('on');
        }
    }
);

http://docs.jquery.com/Events/toggle

$(".wizard-img").toggle(
  function () {
    $(this).find("img").attr({src:"x-on.png"});
  },
  function () {
    $(this).find("img").attr({src:"x-off.png"});
  },
  function () {
    $(this).find("img").attr({src:"x.png"});
  }
);

CSS Sprites would indeed be the way to do this, but just for completeness, here's another option.

Normally I'm the last to reach for a regexp, but I think they'll help here

$('.wizard-img').click(function() {
    var $img = $(this).find('img') ,
        src = $img.attr('src') ,
        onCheck = /\-on\.jpg$/ ,
        offCheck = /\-off\.jpg$/ ,
        normal = /\.jpg$/
    ;

    if(src.match(onCheck)) {
        $img.attr('src', src.replace(onCheck, '-off.jpg'));
    } else if (src.match(offCheck)) {
        $img.attr('src', src.replace(offCheck, '.jpg'));
    } else {
        $img.attr('src', src.replace(normal, '-on.jpg'));
    }
});

FWIW, I suggest you do this using plain CSS and background-image. It sounds to me that this is not really normal images, but rather "buttons".


I have found this plugin useful to do something similar to what you asked:

http://www.malsup.com/jquery/cycle/


$('.wizard-img').click(function() {
    var img = $(this).find('img');
    var src = img.attr('src');

    //image is neither on nor off, so change src to on and return
    if(src != 'x-on.png' && src != 'x-off.png') {
        img.attr('src','x-on.png');
        return false;
    }

    //image is on, so change src to off and return
    if(src == 'x-on.png') {
        img.attr('src','x-off.png');
        return false;    
    }

    //image is off, so change src to x and return
    if(src == 'x-off.png') {
        img.attr('src','x.png');
        return false;    
    }
});

$("img").hover(
    function() { this.src = this.src.replace("_Off", "_On");},
    function() { this.src = this.src.replace("_On", "_Off");
});

http://kaidez.com/tutorial-simple-effective-jquery-image-rollover/


I created plugin swapimg: http://xuannd.wordpress.com/2010/12/03/jquery-swap-images-easy/ use: $('.swapimg').swapimg(); opt: $('.swapimg').swapimg({imgOn:"_on.png", imgOff:".png"});

(function($){
    $.fn.swapimg=function(options){
        var defaults={imgOn:"_on.gif",imgOff:".gif"};
        var options=$.extend(defaults,options);
        var $isOn=false;
        var $obj_index=-1;
        return this.each(
            function(){
                if($(this).is('img')){
                    obj=$(this)
                }
                else{
                    obj=$(this).find('img')
                }
                obj.hover(
                    function(){
                        if(this.src.indexOf('_on')==-1){
                            $isOn=false
                        }else{
                            $isOn=true
                        }

                        if($isOn==false){
                            this.src=this.src.replace(options.imgOff,options.imgOn)
                        }
                    },
                    function(){
                        if($isOn==false){
                            this.src=this.src.replace(options.imgOn,options.imgOff)
                        }
                    }
                )
            }
        )
    }
})(jQuery);

Need Your Help

Android gradle: buildtoolsVersion vs compileSdkVersion

android android-gradle

What's the difference between buildtoolsVersion vs compileSdkVersion in the build.gradle for an Android project?

Twitter Bootstrap typeahead: get context / calling element with `this`

javascript jquery twitter-bootstrap closures typeahead

I am using the Typeahead component of Twitter Bootstrap 2.1.1, and jQuery 1.8.1