views:

2369

answers:

1

Is it possible to determine whether an element has a click handler, or a change handler, or any kind of event handler bound to it using jQuery?

Furthermore, is it possible to determine how many click handlers (or whatever kind of event handlers) it has for a given type of event, and what functions are in the event handlers?

+7  A: 

you can get this information from the data cache.

//log them to the console (firebug, ie8)

console.dir( $('#someElementId').data('events') );

//or iterate them

jQuery.each($('#someElementId').data('events'), function(i, event){

    jQuery.each(event, function(i, handler){

        console.log( handler.toString() );

    });

});

Another way is you can use the following bookmarklet but obviously this does not help at runtime.

redsquare
does this only work for event handlers assigned with jQuery?
Russ Cam
Yes Russ, that i true. But inline handlers are grim and deserve not to be shown!
redsquare
It worked! At least it works with events assigned with jQuery. I haven't tested it with events assigned otherwise. Thank you.
mikez302