FairyQueen
FairyQueen

Reputation: 2373

Is there a way in javascript to detect if an element has any events attached to it?

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??

Upvotes: 4

Views: 2245

Answers (3)

Lesley McMillan
Lesley McMillan

Reputation: 21

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");
}

Upvotes: 2

Vimalnath
Vimalnath

Reputation: 6463

You could try this:

$("element").data("events");

Upvotes: 6

Manuel
Manuel

Reputation: 10303

This should get you a list of events:

jQuery(theElement).data('events');

Upvotes: 6

Related Questions