Diego Ledesma
Diego Ledesma

Reputation: 1372

Automatically unbind event handlers in JQuery

Is it possible to say to JQuery, hey! unbind any event handlers before adding this one, without making an explicit call to unbind?.

Something like

$("jq selector").click(function() { ...}, true);

Where true means I want to unbind all click handlers set to the element. I've came across this on various occasions and on some had strange behaviours due not unbinding event handlers first.

Thanks.

Upvotes: 4

Views: 1043

Answers (3)

Diego Ledesma
Diego Ledesma

Reputation: 1372

There are many ways to do this but I believe the correct would be:

$(selector).unbind("click").click(function() { ... });

I give the credit of the answer to Pointy, who replyed in a comment, but since he didn't add it as an answer I share it to the community.

Upvotes: 0

Shyju
Shyju

Reputation: 218722

use the unbind method

$("jq selector").unbind('click');

If you want to remove all events associated with an element, Call unbind() with no argument and it will removes all handlers attached to the element(s):

$("jq selector").unbind();

Upvotes: 5

Riz
Riz

Reputation: 10246

$(selector).die("click").click(function() { ... });

Upvotes: 1

Related Questions