Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does console.log(document.getElementById( 'blah' ))) give different log messages in Chrome?

Tags:

javascript

dom

I am just messing around and am logging out a div that I have selected with standard javascript but that I have executed inside a jQuery document.ready(fn) block.

$(document).ready(function(){
  console.log(document.getElementById( 'blah' ));
})

I'm really interested to know why sometimes I get...

<div id="blah"></div>

And other times I get...

enter image description here

Seems to log differently randomly.

like image 405
Exitos Avatar asked Dec 01 '14 05:12

Exitos


People also ask

What does console log () do?

The console. log() method outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.

How do I show hidden errors in Chrome?

In Chrome, navigate to Tools > Advanced > Error Console. The error console will open. Select JavaScript and Errors from the two drop downs. To find the error location, expand one of the errors.


1 Answers

This does appear to be random, at least in Chrome. If you want to force it one way or another in the Chrome console you can use console.dir and console.dirxml.

  • console.dirxml will force the output to be like your first example, in xml format
  • console.dir will output like your second example, in object notation.

More examples of console commands in Chrome: https://developer.chrome.com/devtools/docs/console-api

Edit: dirxml also works in Internet Explorer 11 and later but not in Firefox though this shouldn't be an issue as Firefox outputs elements in xml format.

like image 121
Calummm Avatar answered Sep 18 '22 08:09

Calummm