I want to create a simple input on the HTML and show the result to webpage.
for example if I type here it shows "asd"
But I want to display this in the html
If there is a way to save the result to some variable that would be great!
Thank you!
Javascript: console.log to html - Stack Overflow
Print console.log on HTML [duplicate] - javascript
[Javascript]How to print console.log in my HTML Document when opened in browser?
Show console.log() in an HTML element in JavaScript - Stack Overflow
Videos
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());
» npm install console-log-html
Let's say you have a <div id="printFollower"></div> somewhere in your html.
Then do this:
Copyfunction printFollowers(followersSum) {
document.getElementById('printFollower').innerHTML = followersSum;
console.log(followersSum); // you can do print followersSum wherever you want
}
Using this method, you can control where the list shows up better, apply CSS, etc.
I suggest you use 'document.write'
Copyfunction printFollowers(followersSum) {
document.write(followersSum);
}
printFollowers(prompt("Write some text"));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
This will print the text straight into the DOM rather than the console. For more information on document, check out the docs
Hello everyone,
I've started with Javascript some days ago and learned some basic stuff at codeacademy. Now I've been at the part where I learned how to create a very simple Textadventure and I wanted to show that to my friend, by sending him a HTML Document (together with my JS Document) so the only thing he would had to do is to open it in Chrome or Firefox an "play" it.
Now I've added my JS to my HTML and all my prompt and confirm commands are shown perfectly, but everything I wrote in the console.log code just doesnt show up on my HTML-Doc when I open it in google chrome. Since I'm now a little into code I figured that you probably need a specific code to make the console.log messages be visible in your browser. I've searched google, but sadly I didn't find a clear answer. So I hoped that maybe one of you guys could help me.
So does anyone know what I have to do to make my console.log messages appear in my html document?
From the clues in your question I take it you work in a browser environment.
console is an object on the window object. Therefor you can easily replace it. The following code will replace the browser's window.console with a custom window.console which has the method log. The log method will take a string and append it to an element with the id myLog
In your HTML:
<p id="myLog"></p>
In your JS:
window.console = {
log: function(str){
var node = document.createElement("div");
node.appendChild(document.createTextNode(str));
document.getElementById("myLog").appendChild(node);
}
}
console.log('hi');
console.log('there');
This is given that you only log strings. If you log other things, such as object, you probably want to add a type check in the log method and if it's an object, run it through JSON.stringify before adding it to the node.
HTLM:
<body>
<ul id ="list"></ul>
</body>
JAVASCRIPT:
function createMessage(str) {
var newMessage = document.createElement('li').innerHTML = str;
var list = document.getElementById("list");
list.insertBefore(newNode, list.childNodes[0]);
}