78

Tried to search online, but does not look like I can formulate search query properly.

How can I, either with jQuery or just javascript list all the handlers or event listeners that are attached to element(s)/document/window or present in DOM?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
GnrlBzik
  • 3,358
  • 5
  • 27
  • 37
  • 1
    See http://stackoverflow.com/questions/446892/how-to-find-event-listeners-on-a-dom-node and http://stackoverflow.com/questions/7810534/have-any-browsers-implemented-the-dom3-eventlistenerlist/7814692#7814692 – Nickolay Oct 18 '11 at 23:03

4 Answers4

48

In jQuery before 1.8, try using $("#element").data("events")

EDIT:

There is also jQuery extension: listHandlers

Luke
  • 18,811
  • 16
  • 99
  • 115
Laimoncijus
  • 8,615
  • 10
  • 58
  • 81
  • 4
    thank you Laimoncijus, how would you pull that without jquery? may be you could give me link to reading material regarding this subject. – GnrlBzik Mar 04 '10 at 22:13
  • 3
    Seems it is not really possible in current implementation of W3C events model. See: http://www.quirksmode.org/js/events_advanced.html (search for "Which event handlers are registered?") The page there is a little older, so maybe now things got better, can't tell you thins, never tried it myself... – Laimoncijus Mar 04 '10 at 22:32
  • 8
    I have just tried this and it doesn't work..... – kjones1876 Jul 24 '11 at 18:40
  • Worked for me in Chrome. Thanks! – codekoala Apr 06 '12 at 19:11
  • Do you know an non jquery approach? in my unit testing, I always get true for: expect(typeof elem[event]).toBe('function'); – Will Hancock Oct 05 '12 at 11:01
  • not work in jQuery 1.9 and afterwards. – hiway Nov 19 '13 at 03:55
  • 5
    This doesn't work in newer versions of jQuery. Instead you must now use `$._data($('#element')[0], 'events')` **note:** first paramater must be HTML element, not jQuery – Richard Rout Jan 08 '14 at 16:37
  • @Laimoncijus This seems not working anymore? I am trying it in jasmine test but not working. – sandip Jul 20 '16 at 12:52
32

When debugging, if you want to just see if there's an event, I recommend using Visual Event or the Elements" section of Chrome's Developer Tools: select an element and look for "Event Listeners on the bottom right.

In your code, if you are using jQuery before version 1.8, you can use:

$(selector).data("events")

to get the events. As of version 1.8, this is discontinued (see this bug ticket). You can use:

$._data(element, "events")

but this is not recommended since it is an internal jQuery structure, and could change in future releases.

This question has some answers which may be useful, but none of them are particularly elegant in the same way that $(selector).data("events") was.

Community
  • 1
  • 1
Luke
  • 18,811
  • 16
  • 99
  • 115
  • +1 Visual Event is indispensable. I'm frankly surprised this functionality hasn't been subsumed by browsers for their dev tools. – L0j1k Mar 23 '15 at 19:14
24

Without jQuery:

if the listeners were added using elem.addEventListener() method, it is not easy to list these listeners. You can override the EventTarget.addEventListener() method by wrapping it with your own. Then you will have the information, what listeners were registered.

var f = EventTarget.prototype.addEventListener; // store original
EventTarget.prototype.addEventListener = function(type, fn, capture) {
  this.f = f;
  this.f(type, fn, capture); // call original method
  alert('Added Event Listener: on' + type);
}

Working example you can find at http://jsfiddle.net/tomas1000r/RDW7F/

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Tomas Jakubco
  • 256
  • 2
  • 3
3

I just discovered visual event 2:

http://www.sprymedia.co.uk/article/Visual+Event+2

go under the "make it go section" and drag the text link to your bookmark toolbar go to a page that has events and click on the bookmark

tested in FF Mac

Mark
  • 765
  • 8
  • 12
  • This is true, currently does not work with delegated events. Ticket is added to hopefully address this in the future: https://github.com/DataTables/VisualEvent/issues/33 – KoalaBear Nov 08 '13 at 09:07