115

Using Google Chrome, if you console.log an object, it lets you inspect the element in the console. For example:

var a = { "foo" : "bar", "whiz" : "bang" };
console.log(a);

This prints out Object which can be inspected by clicking on arrows next to it. If however I try to log an HTMLElement:

var b = goog.dom.query('html')[0];
console.log(b);

This prints out <html></html> which can not be inspected by clicking on arrows next to it. If I want to see the JavaScript object (with its methods and fields) instead of just the DOM of the element, how would I do that?

Ben Flynn
  • 18,524
  • 20
  • 97
  • 142

3 Answers3

213

Use console.dir:

var element = document.documentElement; // or any other element
console.log(element); // logs the expandable <html>…</html>
console.dir(element); // logs the element’s properties and values

If you’re inside the console already, you could simply type dir instead of console.dir:

dir(element); // logs the element’s properties and values

To simply list the different property names (without the values), you could use Object.keys:

Object.keys(element); // logs the element’s property names

Even though there’s no public console.keys() method, if you’re inside the console already, you could just enter:

keys(element); // logs the element’s property names

This won’t work outside the console window, though.

Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
  • 3
    When I use console.dir() directly in the console, it works. But if I write it in my actual .js file in VS Code, the chrome dev console just logs the name of the file that it was written in and the line number. Do happen to know why? – Maiya Aug 29 '19 at 16:08
35

try this:

console.dir(element)

Reference
[Video] Paul Irish on becoming a console power user.

Ross
  • 14,266
  • 12
  • 60
  • 91
  • +1 I was always using `[[element]]` to create an array so that Chrome was forced to display it as an object... Thanks! – pimvdb Mar 09 '12 at 12:16
  • Great straightforward answer. By 'oldest' sort came in a hair after the other one, hence the accept, but many thanks! – Ben Flynn Mar 09 '12 at 12:34
  • No problems. I don't mind which is accepted, but accepted answer should be the one most helpful to others later. – Ross Mar 09 '12 at 13:18
10

Browser print only HTML part, you can put the element in a object to see DOM structure.

console.log({element})
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Navid Shad
  • 738
  • 7
  • 15