Viewing jQuery DOM Event Bindings With FireBug

Posted October 5, 2009 at 10:27 AM

Tags: Javascript / DHTML

The ease with which we can bind events to DOM elements using jQuery is a huge part of what makes jQuery so awesome. But, as with many things that are wired-up programmatically, when something doesn't work, it can be hard to figure out where it went wrong. One cool tip that I picked up reading Cody Lindley's jQuery Enlightenment book is that jQuery-bound events are actually stored in the data() method using the "events" key. This means that if we need to, we can use FireBug at runtime to inspect the events that are bound to a given DOM element.

 
 
 
 
 
 
 
 
 
 

The one caveat here (and maybe this is just my ignorance to the way in which FireBug works) is that we can't just inspect the DOM element to view the bound events. This is because the data() method doesn't actually store the data with the given DOM element; rather, it stores the data internally to jQuery using a unique ID that it attaches to the given DOM element. As such, to view the bound events, we'll actually need to call the data( "events" ) method on the given DOM element from the FireBug console command line.

But, it gets even more complicated than that - we need a way to reference the given DOM element in a unique way so that we can access it using a jQuery selector. In order to accomplish this, I created a script that uses advanced jQuery filtering to find all the DOM elements that have bound events and add a unique class name to them. This way, I can use the unique class name to access the DOM element from the FireBug console command line.

 Launch code in new window » Download code as text file »

  • <!DOCTYPE HTML>
  • <html>
  • <head>
  • <title>Viewing Events Attached To The DOM</title>
  • <script type="text/javascript" src="jquery-1.3.2.js"></script>
  • <script type="text/javascript">
  •  
  • // When the DOM is ready, initialize scripts.
  • $(function(){
  •  
  • // Attach click event to first link.
  • $( "a.event" )
  • .attr( "href", "javascript:void( 0 )" )
  • .click(
  • function( event ){
  • alert( "Hello There!" );
  •  
  • // Cancel default event.
  • return( false );
  • }
  • )
  • ;
  •  
  •  
  • // Attach the reveal event click event. This will
  • // highlight all of the evented links in gold and
  • // attached a unique class name so that we can
  • // easily grab that link with a jQuery selector.
  • $( "a.reveal" )
  • .attr( "href", "javascript:void( 0 )" )
  • .click(
  • function( event ){
  • var counter = 1;
  •  
  • // Find all of the items on the page that
  • // have events attached to them. We will
  • // use advanced filtering to get this.
  • $( "*" )
  • .filter(function(){
  • // Return only items that have the
  • // events data item.
  • return( $( this ).data( "events" ) );
  • })
  • .css( "background-color", "gold" )
  • .each(
  • function(){
  • $( this ).addClass( "evt" + counter++ );
  • }
  • )
  • ;
  •  
  • // Cancel default event.
  • return( false );
  • }
  • )
  • ;
  •  
  • });
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • Viewing Events Attached To The DOM
  • </h1>
  •  
  • <p>
  • Click
  • <a href="##" class="event">this link</a> and
  • <a href="##" class="event">that link</a>
  • </p>
  •  
  • <p>
  • <a href="##" class="reveal">Reveal event'ed items.</a>
  • </p>
  •  
  • </body>
  • </html>

As you can see above, once the "reveal" link is pressed, all event-having DOM elements are located and assigned a unique class name in the form of "evtX". This class name can then be leveraged to output the bound events:

 Launch code in new window » Download code as text file »

  • $( ".evtX" ).data( "events" );

Of course, you could also just edit the HTML using FireBug, added a custom class name, and do the same thing. Either way, it's cool that this is possible.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Oct 5, 2009 at 11:20 AM // reply »
1 Comments

Much (definitely not all) of the time, the new(ish) versions of Firebug (at least in FF3.5+) already show attached events inline on the HTML tab.


Oct 5, 2009 at 11:21 AM // reply »
14 Comments

That's an interesting tidbit of information, Ben: thanks for sharing it.


Oct 5, 2009 at 11:31 AM // reply »
7,572 Comments

@Christoph,

Oh good to know. I'll have to check to see if I'm rocking the latest version.


Oct 5, 2009 at 12:40 PM // reply »
2 Comments

the FireQuery plugin for FireBug shows all jQuery .data() and such directly in the HTML view of Firebug, along with a bunch of other nifty features. A+++, Would DL Again!

http://firequery.binaryage.com/


Oct 6, 2009 at 5:15 AM // reply »
1 Comments

thx Ben. Love to see your tutorials.


Oct 6, 2009 at 7:48 AM // reply »
7,572 Comments

@ajpiano,

Oh awesome, I'll have to check that out. Thanks!


Oct 14, 2009 at 5:56 AM // reply »
2 Comments

As you said, maybe not that practical, but if you make a bookmarklet out of this, it would be really useful!


Oct 15, 2009 at 8:25 AM // reply »
7,572 Comments

@Francois,

Cool idea. Also, I took at look at FireQuery as recommended by Adam and it is really cool. I would recommend checking it out.


Oct 15, 2009 at 8:47 AM // reply »
2 Comments

Yes, FireQuery is cool, but lately I use Google Chrome & its Inspector (kind of like a built-in Firebug). The boormarklet will be useful for us Chromians.


Nov 30, 2009 at 2:37 PM // reply »
1 Comments

you can use http://getfirebug.com/


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »