0

I haven't been able to solve my problem searching through Stack. I have my live() function below. You can see how I want to stop the button being clicked a second time while the function is running, and then rebind. The button dies unbind, but it doesn't bind again... :S

$('.control-left, .prevActive').live('click',function(){

    var firstSlide = $('.slider li:first-child');

    if(firstSlide.attr('class') != 'active'){   
        $('.control-left, .prevActive').die('click');   
        moveSlider('left');
        $('.control-left, .prevActive').live('click');
    }

});
1
  • 1
    If you're using jQuery 1.7+ then you should use on() in place of live() or delegate(). Nov 30, 2011 at 22:03

3 Answers 3

3

No need to unbind and rebind just use a flag:

var running = false;
$('.control-left, .prevActive').live('click',function(){
    var firstSlide = $('.slider li:first-child');

    if(firstSlide.attr('class') != 'active' && !running){ 
        running = true;
        moveSlider('left');
        running = false;
    }
});
1
  • Thanks, this worked a treat! I see where I was going wrong - quite a silly mistake really :)
    – Sabai
    Dec 3, 2011 at 18:16
1

You are not passing any function to .live.. try this:

$('.control-left, .prevActive').live('click',function hello(){

    var firstSlide = $('.slider li:first-child');

    if(firstSlide.attr('class') != 'active'){   
        $('.control-left, .prevActive').die('click');   
        moveSlider('left');
        $('.control-left, .prevActive').live('click', hello);
    }

});
0
1

Your last line of code does not bind anything to the click handler. You must bind a function

$('.control-left, .prevActive').live('click', function () { code here });

In your situation you would probably want to do something like this

var myspecialclickfunction = function(){

  var firstSlide = $('.slider li:first-child');

  if(firstSlide.attr('class') != 'active'){   
      $('.control-left, .prevActive').die('click');   
      moveSlider('left');
      $('.control-left, .prevActive').live('click', myspecialclickfunction);
};

$(document).ready(function () {
  $('.control-left, .prevActive').live('click',myspecialclickfunction);
});

Also jQuery 1.42 to 1.6x you should be using

in jquery 1.7+

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.