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.
You can remove all event listeners from a DOM element in Javascript by replacing the element with a deep clone of itself. elem. cloneNode(...) will not clone the event listeners of the source element.
According to the jquery Documentation when using remove() method over an element, all event listeners are removed from memory. This affects the element it selft and all child nodes. If you want to keep the event listners in memory you should use .
getEventListeners(obj) is only a Google Chrome specific Command Line Tool feature. This means that you can only use this feature inside Chrome Dev Tools when manually typing into the console. You cannot use this method in your actual JavaScript source code.
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();
andSelf() function is deprecated.
Use add() function:
$(element).add("*").off();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With