Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you unbind all jQuery events from a particular namespace?

Tags:

jquery

events

Is there a "global" unbind function in jQuery, such that I'd be able to remove all bound events from a given namespace? eg:

// assume these are the events bound to different elements $('#foo').bind('click.myNS', ...); $('#bar').bind('keyup.myNS', ...); $('#baz').bind('dblclick.myNS', ...);      // magic occurs here... $.magicalGlobalUnbindFunction('.myNS');  // ...and afterwards, the three binds from above are gone 

All the examples I've seen for unbind require some elements to be selected first. I guess that technically, you could do $('*').unbind('.myNS'), but that seems very inefficient.

like image 725
nickf Avatar asked Feb 07 '11 15:02

nickf


People also ask

What is unbind event in jQuery?

jQuery unbind() Method The unbind() method removes event handlers from selected elements. This method can remove all or selected event handlers, or stop specified functions from running when the event occurs. This method can also unbind event handlers using an event object.

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.


1 Answers

You could add myNS as a class to each of the elements where you want to unbind the events.

like image 163
mikerobi Avatar answered Sep 22 '22 18:09

mikerobi