4

I have a list of links on the page and sometimes they have events attached to them and sometimes not (they just don't do anything). I want to say, 'if this element has no event handlers (basically it doesn't do anything), then add a class of disabled to it. I googled it but didn't find anything for detecting event handlers. Does anyone know of a way to do something like this??

FairyQueen
  • 2,283
  • 7
  • 37
  • 57
  • http://stackoverflow.com/questions/2382994/how-to-check-if-any-javascript-event-listeners-handlers-attached-to-an-element-d – Florian Margaine May 04 '12 at 14:03
  • duplicate? -> http://stackoverflow.com/questions/1515069/jquery-check-if-event-exists-on-element – rt2800 May 04 '12 at 14:03
  • Duplicate Question......http://stackoverflow.com/questions/2382994/how-to-check-if-any-javascript-event-listeners-handlers-attached-to-an-element-d – Pyare Feb 08 '14 at 11:53

3 Answers3

6

This should get you a list of events:

jQuery(theElement).data('events');
Manuel
  • 10,153
  • 5
  • 41
  • 60
6

You could try this:

$("element").data("events");
Vimalnath
  • 6,373
  • 2
  • 26
  • 47
2

I use the following, tested in IE, FF and Chrome:

if(typeof document.getElementById("elementname").onchange === "function"){ 
    alert("has a function");
} else {
    alert("no function");
}
Littm
  • 4,923
  • 4
  • 30
  • 38