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
.
The Firefox developer tools now does this. Events are shown by clicking the "ev" button on the right of each element's display, including jQuery and DOM events.
If you just need to inspect what's happening on a page, you might try the Visual Event bookmarklet.
Update: Visual Event 2 available;
Prototype 1.7.1 way
1:
Prototype.observe
uses Element.addEventListener (see the source code)2: You can override
Element.addEventListener
to remember the added listeners (handy propertyEventListenerList
was removed from DOM3 spec proposal). Run this code before any event is attached:Read all the events by:
And don't forget to override
Element.removeEventListener
to remove the event from the customElement.eventListenerList
.3: the
Element.onclick
property needs special care here:4: don't forget the
Element.onclick
content attribute: these are two different things:So you need to handle it, too:
The Visual Event bookmarklet (mentioned in the most popular answer) only steals the custom library handler cache:
Element overriding may be questionable (i.e. because there are some DOM specific features like live collections, which can not be coded in JS), but it gives the eventListenerList support natively and it works in Chrome, Firefox and Opera (doesn't work in IE7).
There exists nice jQuery Events extension :
(topic source)
It depends on how the events are attached. For illustration presume we have the following click handler:
We're going to attach it to our element using different methods, some which allow inspection and some that don't.
Method A) single event handler
Method B) multiple event handlers
Method C): jQuery
1.3.x
1.4.x (stores the handler inside an object)
(See
jQuery.fn.data
andjQuery.data
)Method D): Prototype (messy)
1.5.x
1.6 to 1.6.0.3, inclusive (got very difficult here)
1.6.1 (little better)