Reputation: 1480
I was disabling a hiperlink using this function, but i don't really understand what really the function does, my doubt is the next one, i have this function:
function bindPrevent(e)
{
e.preventDefault();
return false;
}
and when the user clicks the link i do this:
function disableButton(f)
{
if(f)
{
$('.btnOk[id!="link_lkDelete"]').each(function(){
$(this).bind("click",bindPrevent);
});
}
else
{
$('.btnOk[id!="link_lkDelete"]').each(function(){
$(this).unbind("click",bindPrevent);
});
}
}
The question is, if the link has another click handler, and i bind a new one, then which handler is called??, JQ use something like a handler stack and executes the top one handler??
Upvotes: 2
Views: 227
Reputation: 8144
First, I'd recommend using .on() instead of .bind(), as .on is meant to replace all the binding methods (bind, live, etc.)
To my knowledge, what bind does is watch for an event (such as 'click' or 'mouseenter') and then execute any function you attach to it when it occurs.
Also, if you use return false; you shouldn't need preventDefault(); because return false acts as both preventDefault and stopPropagation.
You could also write the above as:
function disableButton(f)
{
if(f)
$('.btnOk[id!="link_lkDelete"]').click(function(){
return false;
});
}
Upvotes: 1
Reputation: 25796
Like others have stated, both handlers will be called. Here's a fiddle illustrating it.
Upvotes: 1
Reputation: 1931
jquery executes all the handlers asociated with an event.
this is a really good explanation http://jqfundamentals.com/#chapter-5
by using preventDefault you are stopping the event from doing its default action, in this case, following the hyperlink
Upvotes: 2
Reputation: 337733
The question is, if the link has another click handler, and i bind a new one, then which handler is called??, JQ use something like a handler stack and executes the top one handler??
The short answer is that they are both called. They will be called in the order they were assigned.
Under the skin, jQuery simply uses javascripts default mechanism for event attachment, with a bit of fettling to ensure it works in all browsers.
Upvotes: 2