jQuery Selectors - The Firebug Plugin Inserts A Hidden DIV Element

Posted August 27, 2010 at 9:04 AM by Ben Nadel

Tags: Javascript / DHTML

Last night, I was testing out someone's jQuery queue() and dequeue() code and I had a bug that was just driving me crazy; no matter how simple I made my test, my queued callbacks kept firing twice. To give you some insight into how simple my code was, take a look at this demo:

  • <!DOCTYPE html>
  • <html>
  • <head>
  • <title>Hidden FireBug DIV</title>
  • <script type="text/javascript" src="jquery-1.4.2.js"></script>
  • <script type="text/javascript">
  •  
  • // When the DOM is ready, interact with DOM.
  • $(function(){
  •  
  • // Get our testing DIV.
  • var div = $( "div" );
  •  
  •  
  • // Create and add a callback to your div's queue.
  • div.queue(
  • "testQueue",
  • function( next ){
  •  
  • // Simply log that the callback has fired.
  • console.log( "Queued item fired!" );
  •  
  • }
  • );
  •  
  •  
  • // Initiate the dequeuing.
  • div.dequeue( "testQueue" );
  •  
  • });
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • Hidden FireBug DIV
  • </h1>
  •  
  • <div>
  • This is my defined div!
  • </div>
  •  
  • </body>
  • </html>

As you can see, there's almost nothing going on here; when the DOM is ready, I grab the one defined DIV element, queue up a single callback, and then initiate its dequeuing. I'd expect the single queued callback to execute and be done with it. When I run this code, however, I get the following console output in Firebug:

 
 
 
 
 
 
Firebug's Hidden DIV Causes Unexpected Behavior In jQuery Selectors. 
 
 
 

As you can see, jQuery appears to be executing my queued callback twice.

This behavior had me stumped for a good 15 minutes. I tried everything from rewriting the entire demo to searching through the jQuery source code for a queuing bug. Then, finally, I had to the bright idea to double-check the jQuery collection on which I was acting. To do this, I went into the Firebug runtime command line and executed my jQuery selector:

  • $( "div" );

When I did this, I got the following output:

 
 
 
 
 
 
Firebug's Hidden DIV Causes Unexpected Behavior In jQuery Selectors. 
 
 
 

As you can see, when we query the DOM for our "DIV" element, we get back our explicitly defined node plus a hidden one that is added by the Firebug plugin. What's odd is that I can't find that Firebug DIV anywhere in the markup. When I log its parent element, I get "HTML"; however, when I inspect the runtime markup, there is nothing but a HEAD and BODY tag contained within the HTML.

NOTE: This happens regardless of whether or not the Firebug console is actually open.

Normally, this would not be a problem since we very rarely query the DOM for an unqualified DIV element; however, when testing code, it's definitely going to be good to know that Firebug is adding its own hidden markup to the DOM tree.



Reader Comments

Aug 27, 2010 at 9:13 AM // reply »
317 Comments

Another reason to use Chrome. ;)


Aug 27, 2010 at 9:18 AM // reply »
1 Comments

Wow .. this is some really useful information .. Thanks for the heads up ! :)


Aug 27, 2010 at 9:23 AM // reply »
10,743 Comments

@Ray,

I use Chrome for a lot of stuff since it is so much nicer on RAM use and crash prevention. But for development, I just *love* Firebug :)

@Karol,

No problem - I was definitely banging my head against the wall for a few minutes there.


Aug 27, 2010 at 10:14 AM // reply »
1 Comments

As far as I can tell from my understanding Firebug codebase.

Some nodes in the DOM are marked as hidden to Firebug UI (HTML panel, etc.). One example is this _firebugConsole, another is helper DIVs to visualize manual element picking for inspection.

Yes, it may interfere with your page/code in some rare cases. But there was probably no better way how to implement those features. Firebug guys are not code Firefox developers, so they just cannot simply add features/APIs they need for Firebug.


Aug 27, 2010 at 10:31 AM // reply »
211 Comments

Not to mention that it probably helps them maintain the Firebug lite plugin to keep the features available for both the full firebug and for the lite at the same time.


Aug 27, 2010 at 10:31 AM // reply »
10,743 Comments

@Antonin,

Good point about the inspector DIVs as well; I never knew how they did that, but it makes sense. I'm not suggesting that Firebug change the way they do things - as you said, it's just a plugin, not a core Firefox feature. I'm just happy to know how it works so I can work around it.


Aug 27, 2010 at 10:32 AM // reply »
10,743 Comments

@Todd,

Firebug light is an "interesting" beast unto itself :)


Aug 27, 2010 at 10:33 AM // reply »
211 Comments

It has saved my ass more than once when trying to debug something in IE. :/


Aug 27, 2010 at 10:35 AM // reply »
10,743 Comments

@Todd,

I'll agree to that!


Aug 27, 2010 at 11:49 AM // reply »
38 Comments

I've had this happen before and it drove me a little nutty. (A client was reporting that a "promotion" wasn't added to their homepage, but their anti-virus software was rewriting the HTML.)

Here's what I do now:

Use the Web Developer Addon for Firefox, go to "View Source" and then "View Generated Source" and compare it with "View Source" results (or the actual source). It will allow you to determine if any markup is added or removed by other included third-party scripts, Firefox AddOns (not just Firebug; ie AdBlock, LinkAlert, etc) and anti-virus software proxies.


Aug 27, 2010 at 12:04 PM // reply »
12 Comments

Plugins, Grease Monkey Scripts and other things can add to the dom as well. Unfortunately, you can't trust that your html hasn't been altered.


Aug 27, 2010 at 1:37 PM // reply »
1 Comments

Please try Firebug 1.6b1, http://getfirebug.com/releases/firebug/1.6X. We don't need the DIV you see any more. We still need to inject Javascript and elements that support the inspector, but we are working on solutions to remove these eventually as well.


Aug 27, 2010 at 2:11 PM // reply »
1 Comments

In Chrome try the Dev Tools...

Ctrl + Shift + I

really nice...


Aug 28, 2010 at 12:31 AM // reply »
2 Comments

I've seen the div insert by the inspector feature so much... but this never complicated me...

i'm agree with ben nadel, i love firefox/firebug for development too...


Sep 25, 2010 at 1:16 AM // reply »
20 Comments

I've found Firebug Lite in Chrome to do the trick (for the times when the Inspector isn't getting it done).


Sep 28, 2010 at 9:11 PM // reply »
10,743 Comments

@James,

Good thinking with the View Generated Source. I'm not sure I even knew that option existed. I think I may have used it before, but I always forget it.

@Joe,

I've definitely been on the "creating" end of GreaseMonkey scripts; but, I haven't used them in a while. If someone is gonna mess with the markup, there's only so much you can control.

@John,

I'll definitely check out the new FireBug. I love this tool; don't get me wrong - I'm not too concerned about the DIV. After all, I wouldn't normally query the DOM for unspecified tags.

@Franky,

Yeah, people have been telling me that Chrome dev tools are pretty good. I use Chrome a lot, but the dev tools just don't seem to be as easy to use. Maybe I just need to get over that learning curve.

@Acaz,

Exactly - it's a rare case to ever query the DOM for simply, "div." Typically I am using a class or ID selector or a contextual selector.

@Jyoseph,

I've used FBLite in IE for some success. I never thought of using in a browser that has its own development tools.


Sep 29, 2010 at 9:13 PM // reply »
20 Comments

I was under the assumption that the Inspector did not show XHRs like Firebug does. Maybe it's in there, but I've never been able to find it in the inspector. The console in Firebug shows it clearly. That's really the only reason why I still occasionally use Firebug Lite in Chrome.

Example: http://i55.tinypic.com/24v1ik9.png


Sep 29, 2010 at 10:52 PM // reply »
10,743 Comments

@Jyoseph,

Yeah, I think it would be hard for a non-plugin to listen to HTTP requests. Unless there's an event you can bind to (which I don't think there is), I would think you'd have to be part of the core product/plugin-architecture to gain access to that.


Rob
Mar 12, 2012 at 11:20 AM // reply »
1 Comments

Also check out this great resource on creating your own jQuery selectors: http://www.websanova.com/tutorials/jquery/12-awesome-jquery-selector-extensions



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 16, 2012 at 8:18 PM
Best Of ColdFusion 10 Contest Entry - HTML Email Utility
Just found this, looks good! I'm trying to run it on local, it's the 64bit version and I'm experiencing horrible lag. On average the generate.cfm processes the content change in 60-90 seconds. I've ... read »
May 16, 2012 at 6:40 PM
Maintaining Sessions Across Multiple ColdFusion CFHttp Requests
I am trying to integrate this CFHTTPsession into an application that will log into zeekrewards.com to post ads and I am not having any luck. The code works perfectly for logging into other websites, ... read »
May 16, 2012 at 2:44 PM
Creating A Sometimes-Fixed-Position Element With jQuery
Thank you, very useful technique! Worked like a charm. ... read »
May 16, 2012 at 1:58 PM
Movies As A Religious Experience
Acting can, in a way, ruin the movie-goer's experience. I used to be able to get so caught up in movies and their plots, and totally engaged. But lately, I haven't been able to as much with a lot o ... read »
May 16, 2012 at 1:52 PM
The Science Of Optimal Post-Exercise Nutrition
children of this age eat very less vegetables so u can opt for salads they will like it also carrot ,cucumber,onion and as far as pulses are concerned u can boil them ,give him along with mashed rice ... read »
May 16, 2012 at 1:34 PM
Strange ColdFusion JRUN Stack Overflow Error
Hey, Recently I updated my jrun4 using the latest updater 7 and now i am having memory issues :(:(:( any help is appreciated ... read »
May 16, 2012 at 9:56 AM
ColdFusion 10 Beta, Apache Tomcat, And Symbolic Links On Mac OSX
Hi, Now that ColdFusion 10 is out I have stumbled over this as well and I cannot figure out the proper solution. We're running virtual hosts via Apache2; the ColdFusion-applications store their fil ... read »
May 15, 2012 at 6:03 PM
Movies As A Religious Experience
@Ben, I don't know whether you'd consider this a religious observation, but it seems to me, in a sense, movies multiply how many lives we get to have. Each movie is like a little extra life we get ... read »