Bin Chen
Bin Chen

Reputation: 63369

jquery: how to remove the handler setup by .click()?

I found sometimes I hook up 2 functions to the buttun, using .click(function(){}), is it possible for me to remove the prior attached function before I hook up new one?

Upvotes: 4

Views: 1203

Answers (3)

Han Seoul-Oh
Han Seoul-Oh

Reputation: 1296

There are 2 ways:

function foo(){
  //do stuff
}
function bar(){
  //do more
}
jQ.click(foo).click(bar);
jQ.unbind(foo);

or

jQ.bind('click.foo', function(){
  //do stuff
});
jQ.click(function(){
  //do more
});
jQ.unbind('.foo');

Upvotes: 0

Vadim
Vadim

Reputation: 17965

Not using an anonymous delegate like that. You can .unbind a named function though.

UPDATE

Actually you can call .unbind() with no arguments to remove all handlers or .unbind('click') to remove all handlers for a particular event.

Upvotes: 8

jerluc
jerluc

Reputation: 4316

jQuery has unbind()

Upvotes: 2

Related Questions