Awesome Tips For Using Firebug To Navigate And Augment The DOM

Posted May 31, 2011 at 2:39 PM by Ben Nadel

Tags: Javascript / DHTML

Last week, I read JavaScript Web Applications by Alex MacCaw. In that book, Alex really brings the reader through the entire JavaScript application development process, covering topics as tangential as GZipping and Firebug functionality. In fact, he had a few Firebug tips that just about blew my mind. Specifically, Alex pointed out that you could access recently-selected DOM elements or use XPath to programmatically locate DOM elements.


 
 
 

 
 
 
 
 

Firebug allows you to use the selection tool to examine runtime DOM (Document Object Model) nodes in the current HTML document. I use this all the time to look at the applied CSS classes and runtime styles. What I didn't know, however, was that Firebug puts these selected DOM node references into variables that are accessible in the console's command line tool.

Apparently, you can use the variables $0 through $N (where N appears to be vendor specific - ie. Firebug vs. native consoles) to reference the recently selected DOM nodes. These variables can then be used with existing page scripts like jQuery:

  • $( $0 ).css( "background-color", "red" );

This would get the most recently selected DOM node, put it in a jQuery collection, and then turn its background color red.

The other thing that Alex mentioned in the book, which was still awesome though maybe less immediately useful, was the $x() function. This functions allows you to use XPath to query the DOM for a collection of nodes. And, just like the $N variables, this collection can be used in conjunction with existing page scripts like jQuery:

  • $( $x( "//p[ @class= 'intro' ]" ) ).css( "background-color", "gold" );

This would gather up all the P tags that had the class, "intro", put them in a jQuery collection, and then turn their background color gold.

From a technical standpoint, these are really minor tricks; but, I suspect they are the kind of minor tricks that will have a disproportionately huge payoff when put into practice. I can't even tell you how many times I wished that I could reference the last-selected DOM node; and to think, all this time, that functionality was already baked right into Firebug.




Reader Comments

May 31, 2011 at 3:03 PM // reply »
9 Comments

Awesome tip Ben! I like you have used Firebug for years and had no idea about this console functionality. Thanks!


May 31, 2011 at 3:07 PM // reply »
10,743 Comments

@Brian,

My pleasure! When I was reading the book and saw this section, I think I literally said something out loud like, "Holy cow!" I couldn't believe it :)


May 31, 2011 at 6:41 PM // reply »
260 Comments

Tangentially, Remy Sharp's "Debugging Tools" video recently gave me some a-ha moments about Firebug:

http://jqueryfordesigners.com/debugging-tools/


May 31, 2011 at 10:54 PM // reply »
10,743 Comments

@WebManWalking,

That link looks awesome - thanks for sharing.


Jun 1, 2011 at 12:30 AM // reply »
3 Comments

And best of all those tips work in chrome's console. Nice one Ben!


Jun 1, 2011 at 5:41 AM // reply »
8 Comments

There's more in Firebug's Command Line API http://getfirebug.com/wiki/index.php/Command_Line_API.

Also, you should check the Chrome Dev Tools presentation given by Paul Irish and Pavel Feldman this year at Google I/O http://www.youtube.com/watch?v=N8SS-rUEZPg.


Jun 1, 2011 at 6:17 AM // reply »
7 Comments

@Ben
@Edy

Thanks for this tip!

$x(xpath, context) is also present in Chrome Dev Tools. And... $N too :)

Personally, since I have upgraded Firefox to FF4, I stopped to use Firebug. I was tired of crashes, lags of FF...

It's been a while that I use Chrome Tools instead Firebug.

That's remind me that I never finished the Paul Irish's video: http://paulirish.com/2011/a-re-introduction-to-the-chrome-developer-tools/


Jun 1, 2011 at 6:35 AM // reply »
8 Comments

@Loic

Paul Irish is the Man :)
http://vimeo.com/5954529


Jun 1, 2011 at 10:21 AM // reply »
10,743 Comments

@Michael,

Nice - that's good to know. In the book, Alex eluded that it worked in most consoles; but, I never got around to checking that. For me, Firebug is just the cat's pajamas.

@Edy,

It's got some really cool stuff, right?! Thanks for the presentation link, I'll check it out. In my experience a Paul Irish presentation is always good.

@Loic,

I do love Firebug, but I will grant you that: Firebug will definitely slow down your machine after a while. If I'm doing a LOT of JavaScript debugging, I'll probably restart my Firefox at some point, just to let it come to zero for a bit.


Apr 9, 2012 at 9:13 AM // reply »
2 Comments

solved big problem thanks


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2012 at 1:58 AM
Updated: Converting A ColdFusion Query To CSV Using QueryToCSV()
Hi Ben, why do you need to have so many double quotes when adding the field and field name to the row data? ----------------------------------------- <cfset LOCAL.RowData[ LOCAL.ColumnIndex ] = ... read »
AXL
May 21, 2012 at 1:24 AM
URL Rewriting And ColdFusion's WriteToBrowser Image Functionality (CFFileServlet)
@Mounir, Open your lower case URL Rewrite rule and add the following condition. Condition input: {REQUEST_URI} Check if input string: Does Not Match the Pattern Pattern: ^/CFFileServlet/_cf_ca ... read »
May 20, 2012 at 4:28 AM
Understanding The Complex And Circular Relationships Between Objects In JavaScript
@Will Vaughn I tried your javascript example but got this error:- foo.print is not a function ... read »
May 19, 2012 at 5:37 AM
A Graphical Explanation Of Javascript Closures In A jQuery Context
Thanks for this article, but I fear you missed an important point. If variables in the outer context change, these changes affect the inner anonymous functions as well. That means: if you change the ... read »
May 18, 2012 at 3:39 PM
Parsing CSV Data With An Input Stream And A Finite State Machine
Can you use file upload button with this? and read live? or does the file have to already be on the server saved? ... read »
May 18, 2012 at 1:06 AM
VIRGO (Aug. 23-Sept. 22): Dead On The Money!
A friend of mine and I were arguing about astrology and she told me that he believes in astrology. She hasn't provided me with any evidence that the belief makes any sense to me. She she been telling ... read »
May 17, 2012 at 11:32 PM
Using ColdFusion to Handle 404 Errors (Page Not Found) On Development Server
Very easy the configuration. I read a lot pages and I can't find the solution. I open the administrator and change this Administrator/server settings/Error Handlers/Missing Template Handler and p ... read »
May 17, 2012 at 3:13 PM
LOCAL Variables Scope Conflicts With ColdFusion Query of Queries
I never cease to be amazed that almost EVERY random CF issue I come across lands me on your site. Thank you for documenting your findings for the world. ... read »