javascript tutorial - [Solved-5 Solutions] Javascript/jQuery event binding with firebug - javascript - java script - javascript array



Problem:

How to debug JavaScript/jQuery event bindings with Firebug (or similar tool)

Solution 1:

See How to find event listeners on a DOM node . In a nutshell, assuming at some point an event handler is attached to our element (eg): $('#foo').click(function() { console.log('clicked!') }); We 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!') }"
})
click below button to copy the code. By JavaScript tutorial team
  • 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!') }"
})
click below button to copy the code. By JavaScript tutorial team

See jQuery.fn.data (where jQuery stores our 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!') }"
})
click below button to copy the code. By JavaScript tutorial team

Solution 2:

There's a nice bookmarklet called Visual Event that can show we all the events attached to an element. It has color-coded highlights for different types of events (mouse, keyboard, etc.). When we hover over them, it shows the body of the event handler, how it was attached, and the file/line number (on WebKit and Opera). We 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.

Solution 3:

The Eventbug extension has been released yesterday, see here

Solution 4:

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' + we + ': [' + name + '] : ' + handler );
               });
           }
        });
    });
};
click below button to copy the code. By JavaScript tutorial team

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/>
click below button to copy the code. By JavaScript tutorial team

Solution 5:

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

ex:
$._data(document, "events") or $._data($('.class_name').get(0), "events")
click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - Javascript/jQuery event binding with firebug