Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Elements when logging jQuery object in Chrome Dev Tools console?

It seems like something has changed lately with Chrome Dev Tools. Logging a jQuery object with console.log used to show the Element of the DOM node in the console. Something like this:

[<a href="#employees">Employees</a>]

Now it has changed to a clickable object like this:

[<a>, context: <a>]

Is there a way to go back to the old style of object logging or even a different method to call on console?

I'm currently using version 23.0.1271.64. Not sure exactly which version brought the change.

like image 621
David Tuite Avatar asked Nov 25 '12 15:11

David Tuite


People also ask

How do I see variable values in Chrome console?

We can then check what values are stored in any variable at that point of time. Click F12 to open Developer Tools in Chrome. Or we can right-click and select Inspect (Ctrl+Shift+I).

How do I see elements in Chrome?

One of the easiest ways to inspect a specific web element in Chrome is to simply right-click on that particular element and select the Inspect option. Clicking on the Inspect option from the right-click menu will directly open the Developer tools including the editor, Console, Sources, and other tools.

How do I get element in browser console?

Apart from clicking on "More tools-> Developer Tools", we can also open the element box using the following options: Clicking the F12 key. Using keyboard shortcut, "Ctrl + Shift + i" or "Ctrl + Shift + c" on Windows Operating System. The same command works on Chrome OS and Linux.

How do I view console logs in Chrome?

Chrome console logSelect the 'Console'' tab and make sure the option 'Preserve log' is checked. Reproduce the issue. You'll see data being collected in the console window. Right click on any log statement in the console window, and click Save As… to save the log file to your computer.


1 Answers

If you want console.log() to spit out the DOM element, you need to log the DOM element and not the jQuery object. The DOM element is always accessible as the 0th element of the jQuery selector. So, the way you would access the actual DOM element from a jQuery selector is like this:

   $("#someSingleDOMObjectSelector")[0]

And to get the DOM element to appear in the log the way you would like, you would do this:

   console.log($("#someSingleDOMObjectSelector")[0]);

And for jQuery selectors which contain/return multiple DOM elements, you could loop them, like this:

   $('.someMultipleDOMObjectSelector').each(function(){
           //console.log($(this)[0]); //or -->
           console.log(this);
   });

As to why Chrome's dev tools do this, I can only guess that it is done because it makes more sense to log a jQuery object as an object.

like image 62
J.Wells Avatar answered Sep 21 '22 17:09

J.Wells