19

Is there a feature in chrome dev tools(or any extension) by which I can view all the event listeners that are used on a certain page/app.

Edit:
Its certainly not a duplicate of this question : How do I view events fired on an element in Chrome DevTools?

The above question explains how to look for a particular event that gets fired when we interact with our app ( I am aware of how to do that!).

What I am looking for is the List of all the events that we are listening to in the app and which DOM elements they are attached to.

Community
  • 1
  • 1
bhavya_w
  • 9,186
  • 9
  • 29
  • 38
  • Can't you use the source panel: https://developer.chrome.com/devtools/docs/javascript-debugging? – arnolds Feb 06 '15 at 13:28
  • [VisualEvent](https://github.com/DataTables/VisualEvent) - _Visual Event is a Javascript bookmarklet which provides debugging information about events that have been attached to DOM elements. Visual Event shows: Which elements have events attached to them, The type of events attached to an element, The code that will be run with the event is triggered, The source file and line number for where the attached function was defined (Webkit browsers and Opera only)_ – Andreas Feb 06 '15 at 13:43
  • Thanks for suggestion andreas. I already tried it. Looks like it only grab only DOM level 0 and 1 type events and not event listeners. – bhavya_w Feb 06 '15 at 13:47
  • Is utilizing jQuery an option ? – guest271314 Feb 06 '15 at 13:48
  • [EventSpy](http://jfsl.dk/eventspy) - _Event Spy is a chrome extension that lets you spy on the events that are listened for in the application code, this allows you to easily dissect large web applications_ https://www.youtube.com/watch?v=Sl2LPQt1UPc – Andreas Feb 06 '15 at 13:57
  • @guest271314....yes i can use jQuery by all means...please suggest.. – bhavya_w Feb 06 '15 at 13:57
  • See http://stackoverflow.com/questions/24744967/find-elements-with-click-event/24747468#24747468 , http://stackoverflow.com/questions/26225987/one-listener-for-all-events-in-jquery-event-namespace/26227362#26227362 – guest271314 Feb 06 '15 at 13:59
  • Possible duplicate of [How do I view events fired on an element in Chrome Web Developer?](http://stackoverflow.com/questions/10213703/how-do-i-view-events-fired-on-an-element-in-chrome-web-developer) – tjati Feb 11 '16 at 11:44
  • @omeinusch...Please see my edit. – bhavya_w Feb 11 '16 at 13:13
  • You can read about how to do this in the DevTools docs here https://developer.chrome.com/devtools/docs/dom-and-styles#viewing-element-event-listeners. – jaredwilli Feb 19 '15 at 19:53

3 Answers3

36

The Chrome Devtool can't do this for you. But you can inspect those in your console with the API chrome gives: getEventListeners

Just put this code in your dev-tool's console, you can get all the binding click events number in your page:

Array.from(document.querySelectorAll('*'))
  .reduce(function(pre, dom){
    var clks = getEventListeners(dom).click;
    pre += clks ? clks.length || 0 : 0;
    return pre
  }, 0)

The result is like this:

3249

That was a lot of click binding there. Definitely not a good example of project for performance.

If you want see what events have been bound in all your elements in your page and how many of the listeners of each of the events, just put these codes in your dev-tool's console:

Array.from(document.querySelectorAll('*'))
  .reduce(function(pre, dom){
    var evtObj = getEventListeners(dom)
    Object.keys(evtObj).forEach(function (evt) {
      if (typeof pre[evt] === 'undefined') {
        pre[evt] = 0
      }
      pre[evt] += evtObj[evt].length
    })
    return pre
  }, {})

The result is like this:

{
  touchstart: 6,
  error: 2,
  click: 3249,
  longpress: 2997,
  tap: 2997,
  touchmove: 4,
  touchend: 4,
  touchcancel: 4,
  load: 1
}

And so many other info you can get from this API. Just improvise.

Mr.Raindrop
  • 881
  • 9
  • 11
15

Answer in 2021: You can now do this with Chrome Dev Tools! :)

    • Right click on element in page and select "Inspect to open dev tools
    • Alternatively
      • Open Developer Tools (F12)
      • Select "Elements" Tab (first one by default)
      • Select an Element in the HTML page structure
  1. In the right box go to "Event Listeners" (by default 4th next to "Layout")
  2. Check "Ancestors" checkbox and "All" in dropdown to see all the Event Listeners. Optionally select "Framework Listeners".
Zaphoid
  • 2,360
  • 1
  • 18
  • 19
  • Isn't your solution missing the eventlisteners of the decendants of the element, that you described? Find a quick example to try here: https://blittle.github.io/chrome-dev-tools/elements/event-listeners.html – else42.de Apr 12 '22 at 14:00
  • You're right. It doesn't show all the event listeners on the page but only the ones for the selected element (and ancestors, if checked). Still a pretty useful tool ;) – Zaphoid May 31 '22 at 21:22
  • I'm examining the Event Listeners for the selected element in the elements source panel and I see multiple assignments under the events that I am listening for. Is this normal? I do see examples where there is a single assignment, but mostly I am seeing multiples. – kstubs Oct 04 '22 at 23:01
  • This is so much better than the answer from @Mr.Raindrop nowadays as with this DevTools-native way you can even inspect CustomEvents which the snippet will not allow you. Moreover you can remove specific Event Listeners which is super helpful in debugging! – WebDevPassion Oct 26 '22 at 17:16
  • @Zaphoid, Useful tool but it not related to question. The question was to see ALL EVENT LISTENERS for a particular page and not an individual element. Totally different things. – bhavya_w Nov 28 '22 at 11:06
3

Chrome Dev-Tools don't properly show jQuery-added event-listeners.

This library seems to cover this: https://blinkingcaret.wordpress.com/2014/01/17/quickly-finding-and-debugging-jquery-event-handlers/

Finding event handlers registered using jQuery can be tricky. findHandlersJS makes finding them easy, all you need is the event type and a jQuery selector for the elements where the events might originate.

showdev
  • 28,454
  • 37
  • 55
  • 73
Qasim
  • 1,554
  • 1
  • 13
  • 22