J S J S - 9 months ago 88
Javascript Question

Remove All Event Listeners of Specific Type

I want to remove all event listeners of a specific type that were added using

. All the resources I'm seeing are saying you need to do this:


But I want to be able to clear it without knowing what it is currently, like this:



That is not possible without intercepting addEventListener calls and keep track of the listeners or use a library that allows such features unfortunately. It would have been if the listeners collection was accessible but the feature wasn't implemented.

The closest thing you can do is to remove all listeners by cloning the element, which will not clone the listeners collection.

Note: This will also remove listeners on element's children.

var el = document.getElementById('el-id'),
    elClone = el.cloneNode(true);

el.parentNode.replaceChild(elClone, el);