Sabai
Sabai

Reputation: 1599

Rebind a live() click event after using die()?

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');
    }

});

Upvotes: 0

Views: 1177

Answers (3)

John Hartsock
John Hartsock

Reputation: 86882

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+

Upvotes: 1

Esailija
Esailija

Reputation: 140228

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);
    }

});

Upvotes: 1

PetersenDidIt
PetersenDidIt

Reputation: 25620

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;
    }
});

Upvotes: 3

Related Questions