15

I can add or remove an event handler for a DOM node. Is it possible to find out all the registered events handlers of a given DOM node? I am referring to straight Javascript meaning no frameworks or toolkits like jQuery, dojo, Prototype, GWT, etc. If the answer is no, any reason why? Security issues?

Murali VP
  • 6,198
  • 4
  • 28
  • 36
  • possible duplicate of [Have any browsers implemented the DOM3 EventListenerList?](http://stackoverflow.com/questions/7810534/have-any-browsers-implemented-the-dom3-eventlistenerlist) – Nickolay Oct 18 '11 at 23:02
  • A question asked more than 2 years ago is a duplicate of one asked two days ago? – Murali VP Oct 20 '11 at 18:39

7 Answers7

10

I know this is an old question, but just in case, for chrome you can use getEventListeners

getEventListeners function

as mentioned here:

https://stackoverflow.com/a/17466308/538752

roy riojas
  • 2,406
  • 1
  • 28
  • 30
4

DOM Level 3 specifies eventListenerList - however, I'm not aware of any DOM implementation which supports this - or any other reliable way to list the event listeners. It seems to have been an oversight to this point.

TML
  • 12,813
  • 3
  • 38
  • 45
  • 2
    DOM Level 3 doesn't define eventListenerList (you're linking to a draft from 2002). See http://stackoverflow.com/questions/7810534/have-any-browsers-implemented-the-dom3-eventlistenerlist/7814692#7814692 – Nickolay Oct 18 '11 at 22:50
  • @Nickolay Indeed - it's incredibly disappointing to see that the WG can't see the use cases for the API, but thanks for pointing out that it has been removed. – TML Oct 19 '11 at 04:45
  • Please update this answer wrt the "specification". I had to read a comment to be [re]disappointed. – user2246674 Sep 29 '13 at 02:40
3

This works for Chrome/Safari console:

getEventListeners(document.getElementByID('myElementId'));
dasfdsa
  • 7,102
  • 8
  • 51
  • 93
0

If your interest is to discover some event, in order to disable it - I came here because of that - I recommend to use the Firebug extension, with Mozilla Firefox. Selecting the part of the document, you are interested in, look at the right panel, the Events tab: you will see all events, and can even disable them.

Sergio Abreu
  • 2,686
  • 25
  • 20
0

Visual Event can show you which events are registered, but it only works with DOM level 0 attached events; the W3C level 2 implementation as well as the Internet Explorer proprietary method are not supported and/or cannot be retrieved.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
0

Also, in Google Chrome, please select the element and notice the number, it will show you $0 or any other number.

Then in console, type this code and press enter.

getEventListeners($0)

and then you will see the result. Please see the image below for more elaboration.

Google Console showing getEventListeners result

shakeel
  • 1,717
  • 10
  • 14
0

I faced the same problem, landed here, and didn't find an useful answer.

In case you can execute script before addEventListener calls from other parties, you might do something really dirty like:

var obj = something; // Your DOM element you want to watch

var beforeAddEvent = obj.addEventListener;
obj.addEventListener = function() {
  // Do something with arguments here (like storing in an array)
  // arguments[0]: event name
  // arguments[1]: Listener function
  // arguments[3]: eventual options passed

  // If you don't call this, the event listener won't even be attached, it might be also useful in some case
  beforeAddEvent.apply(obj, arguments);
};