Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see which jQuery function is bound to the element?

I found a site, which has some function that I need, in javascript. It's using jQuery, when I click an tag, some function is executed, so jQuery sets a bind for tag. But how can I find out which function is bound with it? Firebug didn't show it to me :(

like image 976
andymcgregor Avatar asked Apr 29 '10 23:04

andymcgregor


2 Answers

If you wanted to say see the click event handler for an element, you'd get the first handler like this:

$("#element").data("events").click[0].handler

This would give you the function running. Here's an example page showing that

Here's an example:

$("a").click(function() {
  alert($("a").data("events").click[0].handler);
});​

On click, this would alert: function() { alert($("a").data("events").click[0].handler); }

This is just an example using click, but whatever you need it for works, mouseenter, focus, whatever the event may be, including custom events.

As an aside, if you wanted to loop over all event handlers for an element or collection, this would work, just change the selector to what you're after (here's the same example updated to include this):

$.each($("a").data("events"), function(i, e) {
  $.each(e, function(j, h) {
    alert('Event: ' + i + '\nHandler:\n' + h.handler);
  });
});

like image 68
Nick Craver Avatar answered Nov 15 '22 23:11

Nick Craver


Have you tried to use FireQuery? Should be installed in everybody's Firefox/Firebug setup.

like image 33
Marcel Jackwerth Avatar answered Nov 15 '22 23:11

Marcel Jackwerth