I have an anchor tag next
made into a \"button\". Sometimes, this tag needs to be hidden if there is nothing new to show. All
A simpler and short solution
(without using the bind/unbind method)
is to add a class to the object if you want to unbind it then remove this class to rebind ex;
$('#button').click( function(){
if($(this).hasClass('unbinded')) return;
//Do somthing
});
$('#toggle').click(function(){
$('#button').toggleClass('unbinded');
});
When you rebind the click event, you're starting all over. You need to provide the function that you intend to handle the events. jQuery doesn't remember them for you after you call "unbind."
In this particular case, it's a little more complicated. The problem is that when you start up those animations you're setting into motion a series of actions that are driven by a timer. Thus, unbinding and rebinding the "click" handler inside the click handler itself really won't help anything. What would help (assuming you don't want to solve the problem with the is(":animated")
trick) would be to unbind the handler and then rebind in the "finish" callback from your animation.
When you set up the "click" handler in the first place, you can separate the declaration of the function from the call to "click()":
var handler = function() {
$(this).whatever();
};
$('a.next').click(handler);
Then, should you end up wanting to unbind/rebind, you can repeat that call to "click" and refer to the "handler" function the same way.
I would just add a check to see if it's animating first:
$('a.next').click(function() {
if (!$(this).is(":animated")) {
// do stuff
}
});