8

Chrome 24 has a new way of outputting objects in console.log().

For example, console.log($("p")); on this jsFiddle example outputs this insanity:

▼[<p>, <p>, <p>, prevObject: jQuery.fn.jQuery.init[1], context: #document, selector: "p"]
  ► 0: <p>
  ► 1: <p>
  ► 2: <p>
  ► context: #document
    length: 3
  ► prevObject: jQuery.fn.jQuery.init[1]
    selector: "p"
  ► __proto__: Object[0]

I can see that it puts the collection of DOM elements at the beginning. But if you try to expand even a simple <p> tag that is mostly empty, it throws up all over you:

▼ 0: <p>
   accessKey: ""
   align: ""
 ► attributes: NamedNodeMap
  ...
  [stopping here for sanity's sake]

So how do I use all this information? My first instinct is to tame it down to how it used to look, but on second thought, there really is a lot of info in there that I might want to have access to. But I'm having a hard time understanding what I'm looking at. Much of it looks like jQuery values. Is this a list of every jQuery value that the object has (or doesn't have)?

Then there's the whole issue of the ► context: thing and the ► __proto__: thing. Once you start drilling down in __proto__ you will never stop. I think it goes infinitely down!

How can I begin to learn how to use this new output?

EDIT: I actually just realized that I'm still using Chrome 23, this isn't something that was introduced in 24. Someone in this thread said it was a Chrome 24 issue, but maybe it's new in 23? At any rate, I only just recently started noticing this on jQuery objects.

EDIT 2: If you're just looking for how to log the old way, try this: (hat tip)

 console.log.apply(console, $("div"));
Community
  • 1
  • 1
brentonstrine
  • 21,694
  • 25
  • 74
  • 120
  • What would be the best output of `console.log($("p"))` for you? Are you happy with Firebug-style output (e.g. `[p, p#bar, p.foo]`) or Opera Dragonfly’s one (e.g. `Object [+

    , +

    , +

    ]`)? I’m working on [similar issue](https://bugs.webkit.org/show_bug.cgi?id=101150#c14), I think I can improve output of jQuery objects as well.

    – NVI Nov 23 '12 at 12:05
  • 1
    Please star code.google.com/p/chromium/issues/detail?id=162570 – NVI Nov 25 '12 at 22:19
  • @NVI Thanks for that link. What I am used to is a straight-up DOM output, as it used to be. E.g. a jQuery object would output a list of each HTML element that matched, and each of those could be expanded to see all the HTML that is present in the nodes--meaning if an element has `id=hey` physically in the tag, then it would show up, but if it doesn't, I don't get `id: ""` as well as every other theoretically possible attribute that isn't actually present. But that's just what I'm used to and found very useful. I'd still like to be able to do that, but I'm hoping to learn how to use this too. – brentonstrine Nov 26 '12 at 20:14

3 Answers3

2

In your demo fiddle, you are logging NodeLists or HTMLCollections. Each of the elements within a NodeList is represented as a numeric index 0, 1, 2, etc. 0 being the first in the NodeList if any elements exist in it.

When you expand the Elements you see all available properties of an HTML Element as defined in DOM Core 3 specification. Refer here for more information about this http://domenlightenment.com/#3.2 and for a list of all properties and their purposes go here: https://developer.mozilla.org/en-US/docs/DOM/element. None of this is new in Chrome.

What is new in the latest Chrome version is the top level logged object, so if you just opened the console and typed:

console.log(window)

Which for me when I just did it logged a preview of what the expanded window object it provides when logging it. So for example, you should see something like:

Window {is_minor: 5, bmi_ie6: false, careers_adselector: "div.hireme, div#hireme"...}

Other NodeLists properties listed below the Elements of the list:

I modified the fiddle slightly and should show the preview of the objects when you view the console http://jsfiddle.net/jaredwilli/H3YWq/2/

You can't really get rid of any of these things either, they're a part of the DOM and will exist always otherwise the NodeType of what you're looking at will be something other than an ElementNode.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
jaredwilli
  • 11,762
  • 6
  • 42
  • 41
0

I'm just as annoyed as you are with this change. I know this isn't ideal, but it will hopefully help you a little bit.

console.log($('p')[0]);

You may have seen that solution before, and then found it didn't work if you were in a loop for instance:

$('p').each(function(){
 console.log($(this)[0]);
});

That doesn't end up giving you the result you were used to either, so I had to use ['context'] instead of [0] like this:

$('p').each(function(){
 console.log($(this)['context']);
});

Again, I'm looking for a better solution myself, but I thought I'd share with you what I've found.

jarodtaylor
  • 471
  • 4
  • 11
  • Thanks. This helps with the annoyance a little and will save a lot of headache. I still want to award the bounty points to someone who can give a more comprehensive answer though. It seems like it is a fairly big unknown at this point, so that may not happen. – brentonstrine Nov 21 '12 at 17:33
0

You want first element so use [0]

As another option, check out Firebug in Firefox. I know, I love chrome too and use it every day, but Firebug is a great plugin and I've never had an issue with it.

jsteinmann
  • 4,502
  • 3
  • 17
  • 21
  • Thanks, I don't actually want the first element. I am not even asking here to go back to the old way (which gave more than the first element). I'm trying to figure out how to understand what it does now and how to use it. – brentonstrine Nov 26 '12 at 20:04