I have a page where some event listeners are attached to input boxes and select boxes. Is there a way to find out which event listeners are observing a particular DOM node and for what event?
Events are attached using:
- Prototype's
Event.observe
; - DOM's
addEventListener
; - As element attribute
element.onclick
.
It is possible to list all event listeners in JavaScript: It's not that hard; you just have to hack the
prototype
's method of the HTML elements (before adding the listeners).Now every anchor element (
a
) will have alastListenerInfo
property wich contains all of its listeners. And it even works for removing listeners with anonymous functions.(Rewriting the answer from this question since it's relevant here.)
When debugging, if you just want to see the events, I recommend either...
If you want to use the events in your code, and you are using jQuery before version 1.8, you can use:
to get the events. As of version 1.8, using .data("events") is discontinued (see this bug ticket). You can use:
Another example: Write all click events on a certain link to the console:
(see http://jsfiddle.net/HmsQC/ for a working example)
Unfortunately, using $._data this is not recommended except for debugging since it is an internal jQuery structure, and could change in future releases. Unfortunately I know of no other easy means of accessing the events.
Fully working solution based on answer by Jan Turon - behaves like
getEventListeners()
from console:(There is a little bug with duplicates. It doesn't break much anyway.)
Usage:
someElement.getEventListeners([name])
- return list of event listeners, if name is set return array of listeners for that eventsomeElement.clearEventListeners([name])
- remove all event listeners, if name is set only remove listeners for that eventUse getEventListeners in Google Chrome:
WebKit Inspector in Chrome or Safari browsers now does this. It will display the event listeners for a DOM element when you select it in the Elements pane.
I was recently working with events and wanted to view/control all events in a page. Having looked at possible solutions, I've decided to go my own way and create a custom system to monitor events. So, I did three things.
First, I needed a container for all the event listeners in the page: that's the
EventListeners
object. It has three useful methods:add()
,remove()
, andget()
.Next, I created an
EventListener
object to hold the necessary information for the event, i.e.:target
,type
,callback
,options
,useCapture
,wantsUntrusted
, and added a methodremove()
to remove the listener.Lastly, I extended the native
addEventListener()
andremoveEventListener()
methods to make them work with the objects I've created (EventListener
andEventListeners
).Usage:
addEventListener()
creates anEventListener
object, adds it toEventListeners
and returns theEventListener
object, so it can be removed later.EventListeners.get()
can be used to view the listeners in the page. It accepts anEventTarget
or a string (event type).Demo
Let's say we want to know every event listener in this current page. We can do that (assuming you're using a script manager extension, Tampermonkey in this case). Following script does this:
And when we list all the listeners, it says there are 299 event listeners. There "seems" to be some duplicates, but I don't know if they're really duplicates. Not every event type is duplicated, so all those "duplicates" might be an individual listener.
Code can be found at my repository. I didn't want to post it here because it's rather long.
Update: This doesn't seem to work with jQuery. When I examine the EventListener, I see that the callback is
I believe this belongs to jQuery, and is not the actual callback. jQuery stores the actual callback in the properties of the EventTarget:
To remove an event listener, the actual callback needs to be passed to the
removeEventListener()
method. So in order to make this work with jQuery, it needs further modification. I might fix that in the future.