tags:

views:

25

answers:

2

I'm having trouble with a piece of code

$(function(){  
 $('input.no').click(function(){  
 $(this).animate({"left" : "80px"}, 150);  
 $(this).removeClass().addClass('click');   });  

 $('input.click').click(function(){  
 $(this).animate({"right" : "0px"}, 150);  
  });  
});  

and here you can see the full code http://pastebin.me/a5b13717c5d7125cd904572c041ce3e1 not working :(

A: 

The reason the second click is not handled is that at the time you bind your handler, there are no input.click elements, so the handler isn't bound.

To make sure the handler is bound after you change the input's class, you need to use live:

$(function() {
  $('input.no').live('click', function() {
    $(this).animate({"left" : "80px"}, 150);  
    $(this).removeClass().addClass('click');  
  });

  $('input.click').live('click', function() {
    $(this).animate({"right" : "0px"}, 150);  
    $(this).removeClass().addClass('no');  
  });
});
Dexter
+1  A: 

Without using live or delegate:

Add a class to the button like: slider-button

$(function() {
  $('.slider-button').bind('click', function() {
    if ($(this).hasClass('no')) {
       $(this).animate({"left" : "80px"}, 150);  
       $(this).removeClass('no').addClass('click');  
    } else {
       $(this).animate({"left" : "0px"}, 150);  
       $(this).removeClass('click').addClass('no');  
    }       
  });    
});
Tim Santeford
not working ? i mean i'm not sure i tryed it in pastebin and it works only for the left:80px part it doesn't come back
andrei
@andrei I made a fix now try
Tim Santeford
@andrei {"left" : "0px"} was needed
Tim Santeford
@andrei if you refresh pastebin you will see it working. Nice effect
Tim Santeford
@Tim Santeford thanks a lot!
andrei