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??
Asked
Active
Viewed 2,237 times
4
-
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 Answers
6
You could try this:
$("element").data("events");

Vimalnath
- 6,373
- 2
- 26
- 47
-
Ok I put this in the console $("#viewSaleSheet").data("events") which returned an object which held a click inside of it, but there are no click events attached to this link. Why is it doing this?? – FairyQueen May 04 '12 at 14:09
-
-
-
@Tanya since you have a `href` , the console is considering it as onclick event,remove that href and now try `$("#viewSaleSheet").data("events")`. you will not see any events binded. – Vimalnath May 05 '12 at 08:41
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

Lesley McMillan
- 21
- 1