I would like to write the console.log output to a div layer.
For example:
document.write(console.log(5+1)); //Incorrect, random example
Can someone give me a solution to my problem?
Thank you.
EDIT:
what i meant is, for example:
console.log("hi");
and it shows the output "hi" on the screen.
Note: An example: http://labs.codecademy.com/#:workspace
The console. log() method in HTML is used for writing a message in the console. It indicates an important message during testing of any program. The message is sent as a parameter to the console.
log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console. log(A);
You can override the default implementation of console.log()
(function () { var old = console.log; var logger = document.getElementById('log'); console.log = function (message) { if (typeof message == 'object') { logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(message) : message) + '<br />'; } else { logger.innerHTML += message + '<br />'; } } })();
Demo: Fiddle
Slight improvement on @arun-p-johny answer:
In html,
<pre id="log"></pre>
In js,
(function () { var old = console.log; var logger = document.getElementById('log'); console.log = function () { for (var i = 0; i < arguments.length; i++) { if (typeof arguments[i] == 'object') { logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(arguments[i], undefined, 2) : arguments[i]) + '<br />'; } else { logger.innerHTML += arguments[i] + '<br />'; } } } })();
Start using:
console.log('How', true, new Date());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With