How do I console.log a jQuery DOM Element in Chrome?
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'));
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, 2020Comments
-
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?
I am in:
Google Chrome 23.0.1271.97 (Official Build 171054) m
-
Jack over 11 yearsThat'll just return the dom element.
-
kidwon over 11 yearsOh pardon me then I misunderstood that it's the DOM el he's after
-
kidwon over 11 yearsWhat does
console.info()
give you? -
pimvdb over 11 yearsI'm not on Chrome at the moment, but
console.log
can log multiple things in a line, so could you tryconsole.log.apply(console, this);
? -
Naftali over 11 years@pimvdb what is
this
in this context? -
pimvdb over 11 yearsSorry, I meant the whole jQuery object. Something like
console.log.apply(console, $selector);
in this case. -
Naftali over 11 years@pimvdb that seemed to work! jsfiddle.net/maniator/ya7As/2 post as an answer ^_^
-
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 over 11 yearsWow. that looks nice too. Can you add these as addendums to your answer?
-
Khaneliman over 7 yearsNice concise way of accomplishing this.