SystemicPlural
SystemicPlural

Reputation: 5789

Is it possible to remove all events attached to an element and its children

I need to be able to remove events attached to an element and all its children even when I don't know what these events are. All the events are attached using jquery.

Upvotes: 14

Views: 12733

Answers (4)

Corfitz.
Corfitz.

Reputation: 1884

This is not an answer, but it is worth mentioning.

I had used a third party plugin without a destroy method. I found this solution but it didn't work at the beginning until I started digging deeper into the documentation.

Note that if the plugin are creating a $(document).on(event, handler) listener on your object, you have to remove the action by writing $(document).off(event, 'element, element *'). Simply writing $(element).find("*").addBack().off(); won't work if the main listener is the document.

Upvotes: 1

Andy E
Andy E

Reputation: 344575

For jQuery 1.8 and later, use

$(element).find("*").addBack().off();

addBack() adds the original list of elements from $(element) back to the internal collection of elements held by the jQuery object (those returned by find("*")). off() removes all attached event handlers, including those using delegation.

If you just want children and not all descendants, use

$(element).children().addBack().off();

See the documentation:


For jQuery 1.7 and lower, use andSelf() instead of addBack(). For jQuery 1.6 and lower, use unbind() and die() instead of off(). For example:

$(element).children().andSelf().unbind().die();

Upvotes: 26

Ahmed El Kilani
Ahmed El Kilani

Reputation: 345

andSelf() function is deprecated.

Use add() function:

$(element).add("*").off();

Upvotes: 2

Zack Bloom
Zack Bloom

Reputation: 8417

Use the .unbind() method on the element.

Upvotes: 0

Related Questions