How do I console.log a jQuery DOM Element in Chrome?

28,625

Solution 1

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();

Solution 2

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

$("div").each(function(){console.log(this)})​

Solution 3

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

is another way

Solution 4

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'))​;
Share:
28,625
Naftali
Author by

Naftali

I AM HERE G+: https://plus.google.com/114132678747122742656/posts Baseball anyone? http://jsfiddle.net/maniator/K3wCM/embedded/result/ Baseball v2 anyone? https://blipit.net/ WOF anyone? http://jsfiddle.net/maniator/XP9Qv/embedded/result/ Maybe some snake? https://snace.herokuapp.com Or even minesweeper? https://mineweeper.herokuapp.com I am am usually here I am writing here Neal @ Miaou #SOreadytohelp

Updated on December 28, 2020

Comments

  • Naftali
    Naftali over 3 years

    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

  • Jack
    Jack over 11 years
    That'll just return the dom element.
  • kidwon
    kidwon over 11 years
    Oh pardon me then I misunderstood that it's the DOM el he's after
  • kidwon
    kidwon over 11 years
    What does console.info() give you?
  • pimvdb
    pimvdb over 11 years
    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);?
  • Naftali
    Naftali over 11 years
    @pimvdb what is this in this context?
  • pimvdb
    pimvdb over 11 years
    Sorry, I meant the whole jQuery object. Something like console.log.apply(console, $selector); in this case.
  • Naftali
    Naftali over 11 years
    @pimvdb that seemed to work! jsfiddle.net/maniator/ya7As/2 post as an answer ^_^
  • pimvdb
    pimvdb over 11 years
    @Neal: I still wasn't completely happy about it, but I noticed console.group. Here's a maybe more convenient one: jsfiddle.net/vMrka/7.
  • Naftali
    Naftali over 11 years
    Wow. that looks nice too. Can you add these as addendums to your answer?
  • Khaneliman
    Khaneliman over 7 years
    Nice concise way of accomplishing this.