Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use getEventListeners in Chrome Dev Tool?

I tried to trace back which function hooked into a click event of .someclass. I open Chrome Dev Tool and type this

getEventListeners(document.querySelector('.someclass'));

The result is this

Object {}

I cannot click and open it to find out the name of the object or the source code that handle click event.

Is there a way to find out?

UPDATE 1:

Followed Krasimir's advise below. There could be only two events: mousemove or mouseover. So how do I find out the exact function handling that event for canvas.overlay? There are just too many to drill down into.

enter image description here

like image 898
HP. Avatar asked Aug 31 '13 03:08

HP.


People also ask

How do I use event listener in Chrome?

Right-click on the search icon button and choose “inspect” to open the Chrome developer tools. Once the dev tools are open, switch to the “Event Listeners” tab and you will see all the event listeners bound to the element. You can expand any event listener by clicking the right-pointing arrowhead.

What is $$ in Chrome console?

jQuery style DOM queries in the console The most popular API jQuery provides is the $ , used for selecting DOM elements. The Chrome dev tools console allows you to make use of that $ selector, and more. Under the hood, $ is an alias to document.

How do I use Chrome Performance Monitor?

To access the Performance tab, navigate to the website you want to profile, then open Chrome DevTools by right-clicking and selecting Inspect. Select the Performance tab inside Chrome DevTools. The easiest way to capture a performance profile is by clicking the Start profiling and reload page icon.

How do I inspect a click event in Chrome?

Open Google Chrome and press F12 to open Dev Tools. Go to Event Listener Breakpoints, on the right: Click on the events and interact with the target element. If the event will fire, then you will get a breakpoint in the debugger.


2 Answers

  1. Open the DevTools
  2. Open the Elements tab and locate your element (.someclass)
  3. There are four tabs per element - Styles, Properties, DOM Breakpoints and Event Listeners
  4. Select Event Listeners

enter image description here

like image 61
Krasimir Avatar answered Oct 21 '22 07:10

Krasimir


You are getting an empty object when calling

getEventListeners(document.querySelector('.someclass'));

probably because the listener isn't hooked up to .someclass element itself (direct event), but to one of it's ancestors (delegated event). There is a good explanation of this here.

You can list both delegated and direct events by calling getEventListeners for specified node and all it's ancestors. This function should do the trick:

getAllEventListeners = function(el) {
 var allListeners = {}, listeners;

 while(el) {
  listeners = getEventListeners(el);

  for(event in listeners) {
   allListeners[event] = allListeners[event] || [];
   allListeners[event].push({listener: listeners[event], element: el});  
  }

  el = el.parentNode;
 }

 return allListeners;
}

However, this will output exactly the same thing as the "Event Listeners" tab (with "Filter" option set to "All nodes") that Krasimir mentioned in his answer.

like image 14
Konrad Dzwinel Avatar answered Oct 21 '22 07:10

Konrad Dzwinel