17

I used to be able to do console.log(somejQueryObj); and it logged in an array all of the DOM elements that are in the object that I could click and go to the inspector.

Now it does something like this:

[prevObject: p.fn.p.init[1], context: , selector: ".next ()"]

which can confuse many people.

How do I make it so that Chrome logs how it used to log jQuery elements?

Here is a fiddle example


I am in:

Google Chrome 23.0.1271.97 (Official Build 171054) m

Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303

4 Answers4

22

Update: I made a jQuery plugin to bring back the old style logging: jquery.chromelog.


You could create a little function to log all elements on one line:

$.fn.log = function() {
  console.log.apply(console, this);
  return this;
};

Usage:

$("...").log();
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 1
    @Neal: I still wasn't completely happy about it, but I noticed `console.group`. Here's a maybe more convenient one: http://jsfiddle.net/vMrka/7/. – pimvdb Dec 26 '12 at 19:10
  • Wow. that looks nice too. Can you add these as addendums to your answer? – Naftali Dec 26 '12 at 19:11
6

To do it for each element so that you can hover over it, try something like this:

$("div").each(function(){console.log(this)})​
lamflam
  • 281
  • 1
  • 4
3
console.log($(...)[0]);

is another way

kidwon
  • 4,448
  • 5
  • 28
  • 45
1

I have found a solution that would log them individually if need be (but it could clutter the log if it is BIG selector):

http://jsfiddle.net/maniator/ya7As/

var log = function($selector) {
    $selector.each(function() {
        console.log(this);
    });
};
log($('selector'))​;
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • I'm not on Chrome at the moment, but `console.log` can log multiple things in a line, so could you try `console.log.apply(console, this);`? – pimvdb Dec 20 '12 at 15:15
  • @pimvdb what is `this` in this context? – Naftali Dec 20 '12 at 15:15
  • 1
    Sorry, I meant the whole jQuery object. Something like `console.log.apply(console, $selector);` in this case. – pimvdb Dec 20 '12 at 15:16
  • @pimvdb that seemed to work! http://jsfiddle.net/maniator/ya7As/2/ post as an answer ^_^ – Naftali Dec 20 '12 at 15:17