Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to debug a web application that uses jQuery to do some fairly complex and messy DOM manipulation. At one point, some of the events that were bound to particular elements, are not fired and simply stop working.

If I had a capability to edit the application source, I would drill down and add a bunch of Firebug console.log() statements and comment/uncomment pieces of code to try to pinpoint the problem. But let's assume I cannot edit the application code and need to work entirely in Firefox using Firebug or similar tools.

Firebug is very good at letting me navigate and manipulate the DOM. So far, though, I have not been able to figure out how to do event debugging with Firebug. Specifically, I just want to see a list of event handlers bound to a particular element at a given time (using Firebug Javascript breakpoints to trace the changes). But either Firebug does not have the capability to see bound events, or I'm too dumb to find it. :-)

Any recommendations/ideas? Ideally, I would just like to see and edit events bound to elements, similarly to how I can edit DOM today.

share|improve this question
We can debug the code using firebug. Install addon to your browser and debug using firebug. Here the site explain all those things getfirebug.com/javascript – Selva Feb 8 at 10:33
2  
I don't understand why this question was closed. It asks how to accomplish a specific, useful, technical goal. I would request that a moderator please leave a comment explaining what is wrong so that the question can be edited to address any deficiency, and hopefully reopened. – antinome Jun 27 at 16:37
2  
@antinome: A few of the mods have been going close-crazy lately. I don't understand it either - I think they are abusing their powers. (a bit ironic, since that all of that mod's top answers are to questions that would have been immediately closed as off-topic, if anyone else had answered them...) This seriously needs to stop, it has gotten way out of control. – BlueRaja - Danny Pflughoeft Jul 1 at 21:38
1  
There are currently more than 56,000 "close question" votes in the queue. Maybe some mods are succumbing to the pressure... – Bennett McElwee Jul 1 at 22:12
add comment (requires an account with 50 reputation)

15 Answers

up vote 243 down vote accepted

See How to find event listeners on a DOM node.

In a nutshell, assuming at some point an event handler is attached to your element (eg): $('#foo').click(function() { console.log('clicked!') });

You inspect it like so:

  • jQuery 1.3.x

    var clickEvents = $('#foo').data("events").click;
    jQuery.each(clickEvents, function(key, value) {
      console.log(value) // prints "function() { console.log('clicked!') }"
    })
    
  • jQuery 1.4.x

    var clickEvents = $('#foo').data("events").click;
    jQuery.each(clickEvents, function(key, handlerObj) {
      console.log(handlerObj.handler) // prints "function() { console.log('clicked!') }"
    })
    

See jQuery.fn.data (where jQuery stores your handler internally).

  • jQuery 1.8.x

    var clickEvents = $._data($('#foo')[0], "events").click;
    jQuery.each(clickEvents, function(key, handlerObj) {
      console.log(handlerObj.handler) // prints "function() { console.log('clicked!') }"
    })
    
share|improve this answer
161  
alert() is bad bad bad for debugging. It's a horrorshow for asynchronous events and trecherous in animation or mouseevent stuff. console.log() always gives more information, it's formatted nicely and its non-modal. Always use console.log, die alert die. – Paul Irish Dec 20 '10 at 0:09
3  
How would this look like for jQuery 1.6? – Pilgrim Sep 15 '11 at 14:05
8  
FYI: This will not display events that weren't attached with jQuery – Juan Mendes Oct 7 '11 at 18:39
5  
Totally agree about console.log(), however it should be hedged with something like if (window.console) in case it gets left in the code (much easier to do than with alert()) and breaks IE. – thepeer Jan 24 '12 at 16:46
8  
@thepeer Personally I prefer to do a check for the console at the start of the file, and if it doesn't exist create a dummy object. – Andrew Feb 4 '12 at 13:15
show 5 more commentsadd comment (requires an account with 50 reputation)

There's a nice bookmarklet called Visual Event that can show you all the events attached to an element. It has color-coded highlights for different types of events (mouse, keyboard, etc.). When you hover over them, it shows the body of the event handler, how it was attached, and the file/line number (on WebKit and Opera). You can also trigger the event manually.

It can't find every event because there's no standard way to look up what event handlers are attached to an element, but it works with popular libraries like jQuery, Prototype, MooTools, YUI, etc.

share|improve this answer
1  
This bookmarklet seems to have stopped working and their website is down :-(. It seems that the bookmarklet calls a Javascript file that was on their site – Casebash Jul 18 '10 at 23:19
4  
@Casebash: It's working again. – Matthew Crumley Jul 21 '10 at 3:22
2  
awesome gem! thanks for sharing. – Sam3k Oct 20 '10 at 21:15
3  
This is epic. I wish I had seen this sooner. – a paid nerd Dec 15 '10 at 14:26
4  
Note that since this runs in content JavaScript, it gets its data by querying JavaScript libraries. So it will only show events added with a supported library (which includes jQuery). – Matthew Flaschen Sep 25 '11 at 4:57
show 6 more commentsadd comment (requires an account with 50 reputation)

The Eventbug extension has been released yesterday, see: http://www.softwareishard.com/blog/firebug/eventbug-alpha-released/

Honza

share|improve this answer
Integrates great in Firebug. Thanks for the suggestion! – Husky Sep 29 '10 at 9:11
Jan thank you! No, really! – Liutauras Feb 11 '11 at 16:48
add comment (requires an account with 50 reputation)

You could use FireQuery. It shows any events attached to DOM elements in the Firebug's HTML tab. It also shows any data attached to the elements through $.data.

share|improve this answer
1  
This is a great plugin, thanks! – Jeff Olson Sep 30 '10 at 20:16
That plugin has 1 really big downside: When you are debugging, and you want to inspect the value of a variable which contains a jquery collection you are not able to inspect the value when your code is paused. This is not the cause with firebug. The reason for me to uninstall it. alone – BlackHawkDesign Apr 16 '12 at 12:11
add comment (requires an account with 50 reputation)

Here's a plugin which can list all event handlers for any given element/event:

$.fn.listHandlers = function(events, outputFunction) {
    return this.each(function(i){
        var elem = this,
            dEvents = $(this).data('events');
        if (!dEvents) {return;}
        $.each(dEvents, function(name, handler){
            if((new RegExp('^(' + (events === '*' ? '.+' : events.replace(',','|').replace(/^on/i,'')) + ')$' ,'i')).test(name)) {
               $.each(handler, function(i,handler){
                   outputFunction(elem, '\n' + i + ': [' + name + '] : ' + handler );
               });
           }
        });
    });
};

Use it like this:

// List all onclick handlers of all anchor elements:
$('a').listHandlers('onclick', console.info);

// List all handlers for all events of all elements:
$('*').listHandlers('*', console.info);

// Write a custom output function:
$('#whatever').listHandlers('click',function(element,data){
    $('body').prepend('<br />' + element.nodeName + ': <br /><pre>' + data + '<\/pre>');
});

Src: (my blog) -> http://james.padolsey.com/javascript/debug-jquery-events-with-listhandlers/

share|improve this answer
add comment (requires an account with 50 reputation)

The WebKit Developer Console (found in Chrome, Safari, etc.) lets you view attached events for elements.

More detail in this Stack Overflow question

share|improve this answer
It does, thanks for the tip! – BlackHawkDesign Apr 16 '12 at 12:17
add comment (requires an account with 50 reputation)

Use $._data(htmlElement, "events") in jquery 1.7+;

ex:

$._data(document, "events") or $._data($('.class_name').get(0), "events")

share|improve this answer
add comment (requires an account with 50 reputation)

Hope this will help somebody. Splinetech javascript debugger is what I've been using for years to debug jquery.

share|improve this answer
add comment (requires an account with 50 reputation)

As a colleague suggested, console.log > alert:

var clickEvents = $('#foo').data("events").click;
jQuery.each(clickEvents, function(key, value) {
    console.log(value);
})
share|improve this answer
add comment (requires an account with 50 reputation)

There are a few very good tools for debugging jquery running as Firefox plugins. I wrote an article about this on my blog at http://johnayling.com/programming-tips/debugging-jquery-code.

If you look halfway down the article I show you how to use firefinder to debug jquery events.

share|improve this answer
1  
You might consider summarizing the relevant bits of your blog posting for the benefit of those reading the answers here. It's fine to include a link to your blog for more context or detail. But a direct answer is always appreciated, and based on your link you have a good bit to share here. – GargantuChet Jul 5 '11 at 4:35
add comment (requires an account with 50 reputation)

jQuery stores events in the following:

$("a#somefoo").data("events")

Doing a console.log($("a#somefoo").data("events")) should list the events attached to that element.

share|improve this answer
add comment (requires an account with 50 reputation)

According to this thread, there is no way in Firebug to view what events are attached to listeners on a DOM element.

It looks like the best you can do is either what tj111 suggests, or you could right-click the element in the HTML viewer, and click "Log Events" so you can see which events are firing for a particular DOM element. I suppose one could do that to see what events could be firing off particular functions.

share|improve this answer
add comment (requires an account with 50 reputation)

Looks like FireBug crew is working on an EventBug extension. It will add another panel to FireBug - Events.

"The events panel will list all of the event handlers on the page grouped by event type. For each event type you can open up to see the elements the listeners are bound to and summary of the function source." EventBug Rising

Although they cannot say right now when it will be released.

share|improve this answer
add comment (requires an account with 50 reputation)

The bookmarklet on this page:

jQuery event tracer

will log jQuery events for the page you are viewing to the console.

share|improve this answer
add comment (requires an account with 50 reputation)

I also found jQuery Debugger in the chrome store. You can click on a dom item and it will show all events bound to it along with the callback function. I was debugging an application where events weren't being removed properly and this helped me track it down in minutes. Obviously this is for chrome though, not firefox.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.