views:

91

answers:

2

I have a jQuery requirement like:

I have an Image. If I click it once its size will get reduced. If I again click it it will again resize.

Do we have any functionality to do it easily in jQuery? Or do I have to set a flag and then work on mouseclick? Something like

  $("#img1").click(function() {
      $("#img1").addClass("img1");
  });
+3  A: 

jQuery toggle function might help.

In short=>

CSS:

.small{width: 10px;}

Javascript:

var makeSmall = function(){
         $(this).addClass("small");
     };

var makeNormal = function () {
     $(this).removeClass("small");
    };

$("#id").toggle(makeSmall, makeNormal);

Also - you might want to change CSS directly through jQuery:

var makeSmall = function(){
    $(this).css({'width' : '10px'});
}

P.s. Thinker's approach with toggleClass is cleaner.

Arnis L.
Thnaks Arnis, u explained Toggle in a real simple manner.I got the point.
Wondering
+5  A: 

Don't use toggle, when there is toggleClass function :)

$("#img1").click(function() {
  $("#img1").toggleClass("img1");
});
Thinker
Thanks - i didn't know about this. :)
Arnis L.
Too Good.Thanks for ur help.
Wondering
jQuery rox! ^^
Arnis L.