11

Possible Duplicate:
How to find event listeners on a DOM node?

Given an element, let's say a div, is it possible to obtain a list of the handlers bound to a specific event fired by the element?

/* pseudo code: click on a div handlers? */
document.getElementById('myDiv').getHandlers('click');

Edit

Ok, here's some more details. I want to bind a click handler to a div, unless another handler is already bound to the div. What I have in mind is the following:

/* pseudo code: click on a div handlers? */
if(!document.getElementById('myDiv').getHandlers('click'))
   document.getElementById('myDiv').addEventListener('click', myEventhandler);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
  • This is why when I bind handlers I give them unique names. You can even store the name in a data attribute on the element. – Travis J Nov 12 '12 at 23:33
  • @Bergi: I have already read suggested duplicates. But in no case I found a programmatically way to obtain a list of the handlers. Suggested answer regards debugging or using jQuery. – Alberto De Caro Nov 12 '12 at 23:49
  • @ADC: Yes, because there is none. – Bergi Nov 13 '12 at 00:18
  • It's not possible if you are using pure DOM, but possible if you use framework like jQuery. This has already been answered in more details here: http://stackoverflow.com/questions/446892/how-to-find-event-listeners-on-a-dom-node (see second answer) – pixelfreak Nov 12 '12 at 23:37

1 Answers1

10

No, it is not possible with DOM methods.

However, some libraries store all handlers they bind in element-associated data structures to enable methods like unbindAll.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375