Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there tools / techniques to debug jQuery event handlers?

I am currently trying to figure out what click event handlers have attached to my div. There should only be one handler attached, but there appears to be at least one. I'm using FireBug but Chrome could be an option as well. I don't like IE so I'd prefer not to use that.

The best case scenario is that I can inspect my div using FireBug, and see a list of event handlers.

like image 920
MedicineMan Avatar asked Mar 04 '11 19:03

MedicineMan


People also ask

Which jQuery method is used to attach a handler to an event?

Commonly Used jQuery Event Methods The click() method attaches an event handler function to an HTML element. The function is executed when the user clicks on the HTML element.

How is an event handled in jQuery?

Whenever a jQuery event is fired, jQuery passes an Event Object to every event handler function. The event object provides various useful information about the event.

What method in jQuery which is used to point a specified event handler on to a selected element?

bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.


4 Answers

Visual Event is a nice javascript bookmark you can run on a page to see all the events that are attached to a control.

like image 63
bpruitt-goddard Avatar answered Sep 23 '22 00:09

bpruitt-goddard


If you are using jQuery to assign your event handlers you can locate the attached events via $("div").data("events");

$("div.test").each(function(){     var events = $(this).data("events");     //events.click will give you a list of click handlers, events.mouseenter, etc..     $.each(events, function(i, eventType){         $.each(eventType, function(){           //this.handler() can be used to find the anonymous function assigned to the element.           $("body").append("<h1>" + this.type + "</h1>");         });     }); }); 

If you wish to get live handlers you can do the same thing for the document.

var liveEvents = $(document).data("events").live; $.each(liveEvents, function() {     $("body").append("<h1>" + this.selector + this.origType +  "</h1>"); }); 

Example on jsfiddle.

like image 39
Mark Coleman Avatar answered Sep 26 '22 00:09

Mark Coleman


Install fireQuery plugin for firebug and you will see all jQuery-attached events in the firebug display, just like you wish ;)

You can find it here: http://firequery.binaryage.com/ or here: https://addons.mozilla.org/en-us/firefox/addon/firequery/

It will make your firebug look like this: enter image description here

like image 32
Martin Jespersen Avatar answered Sep 22 '22 00:09

Martin Jespersen


Check out the $.data function.

Specifically something like this:

var clicks = $(mySelector).data('events').click;

Should return a list of click event handlers and their associated DOM objects.

like image 25
Ryan O'Neill Avatar answered Sep 23 '22 00:09

Ryan O'Neill