Rebind a live() click event after using die()?
Question
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');
}
});
Solution
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;
}
});
OTHER TIPS
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);
}
});
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
- delegate() for binding to future elements
- undelegate() for unbinding to for delegated events
in jquery 1.7+
- delegate(), bind(), and live() have been combined in the on()
- undelegate(), unbind() and die() have been combined in the off()
- in addition there has been the one() added that will allow a bound event to run only once.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow