javascript tutorial - [Solved-5 Solutions] How to find event listeners on a DOM node when debugging or from the javascript code ?
- javascript - java script - javascript array
Problem:
We 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:
1.6 to 1.6.0.3, inclusive (got very difficult here)
1.6.1 (little better)
Solution 3:
Chrome, Firefox, Vivaldwe and Safarwe support getEventListeners(domElement) in their Developer Tools console.
For majority of the debugging purposes, this could be used.
Solution 4:
It is possible to list all event listeners in JavaScript: It's not that hard; we just have to hack the prototype's method of the HTML elements (before adding the listeners).
Now every anchor element (a) will have a lastListenerInfo property wich contains all of its listeners. And it even works for removing listeners with anonymous functions.
Solution 5:
When debugging, if we just want to see the events, we recommend either...
The Elements section of Chrome's Developer Tools: select an element and look for "Event Listeners" on the bottom right (similar in Firefox)
If we want to use the events in our code, and we are using jQuery before version 1.8, we can use:
To get the events. As of version 1.8, using .data("events") is discontinued (see this bug ticket). We can use:
Another example: Write all click events on a certain link to the console:
Unfortunately, using $._data this is not recommended except for debugging since it is an internal jQuery structure, and could change in future releases. Unfortunately we know of no other easy means of accessing the events.