Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the jQuery .unbind() method only work on jQuery created events?

I am trying to unbind all event handlers for all elements that are inside a particular container. Like a DIV. But those events have been bound/registered not using jQuery. Some are bound the manual way with onclick="...." or using regular native JavaScript.

But when I do something like this

$('#TheDivContainer').find('div,td,tr,tbody,table').unbind();

It does not appear to work. Which leads me to believe that the .unbind() only works if the events have been originally bound by jQuery.

Is that true? Is there another way of unbinding all events from a group of elements ?

Thanks!

like image 241
7wp Avatar asked Jun 01 '10 19:06

7wp


People also ask

Does jQuery remove unbind events?

jQuery unbind() MethodThe 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.

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.

What is the purpose of using bind () method in jQuery?

The bind() is an inbuilt method in jQuery which is used to attach one or more event handlers for selected element and this method specifies a function to run when event occurs. event: This is an event type which is passed to the selected elements.

Which event is removed in jQuery?

If a simple event name such as "click" is provided, all events of that type (both direct and delegated) are removed from the elements in the jQuery set.


3 Answers

You are right. As in the API:

Any handler that has been attached with .bind() can be removed with .unbind().

like image 84
Mikulas Dite Avatar answered Sep 27 '22 23:09

Mikulas Dite


Unbind will only work on jQuery created events as all methods that does this (addEventListener, and attachEvent) requires the both the node, the eventname, and the handler as an argument. bind takes care of storing these for you..

By the way, DOM0 style event listerens (.foo = function(...) can only by removed by setting the same property to something else like null.

like image 21
Sean Kinsey Avatar answered Sep 27 '22 22:09

Sean Kinsey


You could always do this:

$('#TheDivContainer').find('div,td,tr,tbody,table')
  .unbind('click')
  .attr('onclick', ''); // edited to change null to ''

etc. for all appropriate event types.

like image 39
Pointy Avatar answered Sep 27 '22 22:09

Pointy