Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to unbind all event using jquery

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

like image 640
zjm1126 Avatar asked Aug 25 '10 19:08

zjm1126


People also ask

How do I remove all event listeners?

To remove all event listeners from an element: Use the cloneNode() method to clone the element. Replace the original element with the clone. The cloneNode() method copies the node's attributes and their values, but doesn't copy the event listeners.

What is bind and unbind in jQuery?

jQuery bind() function is used to attach an event handler to elements, while the unbind() is used to detached an existing event handler from elements.


2 Answers

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!'); }); 
like image 33
totallyNotLizards Avatar answered Sep 29 '22 08:09

totallyNotLizards


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.

like image 112
Nick Craver Avatar answered Sep 29 '22 07:09

Nick Craver