tags:

views:

300

answers:

3

I need to find which event handlers an object has registered.

eg.

$("#el").click(function(){...});
$("#el").mouseover(function(){...});

Is there anyway I can use a function to find out that-
$("#el") has click and mouseover registered and possibly iterate over the event handlers.
If not a jQuery Object can we find this on a plain DOM object?

+4  A: 

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

I believe.

Jud Stephenson
+1  A: 

You can do it like this:

$("#el").click(function(){ alert("click");});
$("#el").mouseover(function(){ alert("mouseover"); });

$.each($("#el").data("events"), function(i, e) {
    alert(i);
});
//alerts 'click' then 'mouseover'

If you're on jQuery 1.4+, this will alert the event and functions bound to it:

$.each($("#el").data("events"), function(i, event) {
    alert(i);
    $.each(event, function(j, h) {
        alert(h.handler);
    });
});
//alerts: 
//'click'
//'function (){ alert("click"); }'
//'mouseover'
//'function(){ alert("mouseover"); }'

​You can play with it on jsFiddle here

Nick Craver
A: 

Thanks for the answers!!

ages04