Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unbind all events inside a particular div

I have a parent div , say with id="to-remove"

This div has multiple children divs and each children div also can have in-turn multiple divs and each of them can have href and/or onclick events.

Is there way to remove all these events from inside this parent div using jquery..?

like image 417
Rahul garg Avatar asked Dec 21 '11 12:12

Rahul garg


People also ask

How do you unbind an event?

Select the selector on which the event handler is to be removed. Use the unbind() method to remove event. After click on the function under which unbind works will remove the event handler.

How to unbind event in javascript?

Use the off() method instead. 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.

Which method is used to remove Eventbind?

The unbind() Method is an inbuilt method in jQuery which is used to remove any selected event handlers. This method can be used to remove particular event handler, or stop specific functions. It works on any event handler 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

Did you try .unbind():

$('#to-remove *').unbind('click'); // just for click events
$('#to-remove *').unbind(); // for all events

And as of jQuery 1.7, the .on() and .off() methods are preferred to attach and remove event handlers on elements. (from .unbind() documentation) So if you're using jQuery > 1.7.x then this would be better:

$('#to-remove *').off();
like image 141
Emre Erkan Avatar answered Oct 19 '22 12:10

Emre Erkan