how to unbind all event using jquery

JqueryEventsUnbind

Jquery Problem Overview


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

Jquery Solutions


Solution 1 - Jquery

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.

Solution 2 - Jquery

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!');
});

Solution 3 - Jquery

@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.

Solution 4 - Jquery

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

$(document).off().find("*").off();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionzjm1126View Question on Stackoverflow
Solution 1 - JqueryNick CraverView Answer on Stackoverflow
Solution 2 - JquerytotallyNotLizardsView Answer on Stackoverflow
Solution 3 - JqueryBlue RaspberryView Answer on Stackoverflow
Solution 4 - JqueryVikas KandariView Answer on Stackoverflow