83

i can use this code to remove click event,

$('p').unbind('click')

but , has some method to remove all event ?

has a method named unbindAll in jquery ?

thanks

zjm1126
  • 34,604
  • 53
  • 121
  • 166
  • It would be worth changing your accepted answer from Nick's to totallyNotLizards' because `.unbind()` is deprecated. – Clonkex Feb 11 '19 at 02:05

4 Answers4

115

You can call .unbind() without parameters to do this:

$('p').unbind();

From the docs:

In the simplest case, with no arguments, .unbind() removes all handlers attached to the elements.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 7
    If 'p' has some child elements, will this statement unbind listeners on those too? – Vrishank May 05 '16 at 08:30
  • Accroding to the document, from jQuery 3.0, `.unbind()` has been deprecated. It was superseded by the `.off()` method since jQuery 1.7, so its use was already discouraged. – Chester Chu Feb 21 '23 at 04:02
89

As of jQuery 1.7, off() and on() are the preferred methods to bind and unbind event handlers.

So to remove all handlers from an element, use this:

$('p').off();

or for specific handlers:

$('p').off('click hover');

And to add or bind event handlers, you can use

$('p').on('click hover', function(e){
    console.log('click or hover!');
});
totallyNotLizards
  • 8,489
  • 9
  • 51
  • 85
  • 32
    If you want to _remove every binding from your page_ you can call `$(document).add('*').off();`. – flu Feb 21 '13 at 14:26
  • $(document).add('ID_OF_THE_ELEMENT').off(); helped me .. thanks @flu – Mohit Singh Jun 30 '15 at 06:25
  • 3
    @Moitt Glad I could help. Please note, that in your case you could have used `$('#ELEMENT_ID').off()` directly instead. – flu Jun 30 '15 at 07:42
  • Yeah i forgot to mention '#' <-- hash sign .. MY BAD grrrrrrrrrrrrrrrrrrrr... thanks @flu – Mohit Singh Jul 01 '15 at 10:44
  • 1
    @Moitt Yes, that too ;) But I was talking about the `$(document).add(...)` part. You don't need the document in your node list. If you simply want to remove all events on one particular element you know the ID of, then you can use `$('#ELEMENT_ID').off()` right away (nothing added in front of it). – flu Jul 06 '15 at 14:56
3

To remove all event bindings from all elements including document use :

$(document).off().find("*").off();
Vikas Kandari
  • 1,612
  • 18
  • 23
2

@jammypeach is right about on & off being the accepted methods to use. Unbind sometimes ends up creating weird behaviors (e.g. not actually unbinding events correctly).

To unbind all elements within the body, find them all and for each one turn off the click handler (what was the old unbind):

$("body").find("*").each(function() {
    $(this).off("click");
});

Also see how to save the events that you've turned off in this stack overflow question.

Community
  • 1
  • 1
Blue Raspberry
  • 811
  • 7
  • 6