Purpose: This would provide a way to get all event listeners added to an element via
Returns: Array of objects. Each object contains properties for
options for the arguments that registered the corresponding event listener. Events registered via inline event handlers are not included.
Use cases: This would enable removing events based on arbitrary criteria, instead of requiring a reference to the callback, which causes unnecessary couplings. Typically libraries deal with this by providing their own abstractions for adding events that track the listeners manually. However, this is fragile, as it means listeners not registered via the library cannot be retrieved or removed. Some libraries deal with this by hijacking
addEventListener to keep track of listeners, but this is very intrusive for a library and it doesn’t help with any listeners registered before the library was included. Browsers already keep track of event listeners, so it should be relatively easy to expose them, and is on par with the Extensible Web Manifesto principle of exposing browser “magic” via JS APIs.
Edit: WHATWG proposal: https://github.com/whatwg/dom/issues/412