Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if any JavaScript event listeners/handlers attached to an element/document? [duplicate]

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?

like image 865
GnrlBzik Avatar asked Mar 04 '10 21:03

GnrlBzik


People also ask

How do you check if an element already has an event listener JavaScript?

To check if an element has event listener on it with JavaScript, we can call the getEventListeners function in the Chrome developer console. getEventListeners(document. querySelector("your-element-selector")); in the Chrome developer console to select the element we want to check with querySelector .

How do you check event listeners on elements?

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.

Can you have multiple event listeners of the same type?

You can add many event handlers to one element. You can add many event handlers of the same type to one element, i.e two "click" events. You can add event listeners to any DOM object not only HTML elements. i.e the window object.

How do you find out which JavaScript Events fired?

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.


2 Answers

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

EDIT:

There is also jQuery extension: listHandlers

like image 150
Laimoncijus Avatar answered Sep 17 '22 15:09

Laimoncijus


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.

like image 41
Luke Avatar answered Sep 18 '22 15:09

Luke