If the title is not clear...I have BIG function, and I want to see the value of a variable partway through its execution on the webpage... very similar to console.log(). Is there a method to capture that and display it in the HTML ? Thanks.
Asked
Active
Viewed 2,169 times
0
-
Does this answer your question? [How to output javascript to a page](https://stackoverflow.com/questions/36104836/how-to-output-javascript-to-a-page) – TylerH Aug 04 '22 at 17:13
-
See also https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – TylerH Aug 04 '22 at 17:13
3 Answers
1
I think you can achieve it with generator functions
-
Generator functions are an advanced feature of javascript and not practical for a situation like this, IMHO – Ringo Jan 30 '22 at 06:25
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 30 '22 at 09:59
1
The answer has been found, have attached it below ! Thanks everybody.
console.log = function(message) {
document.getElementById('result').innerHTML = message;
};
console.log('I love Panda');
<div id="result"></div>

Ebe Gas
- 109
- 8
-
2This is fine, glad you're happy with it. But you probably shouldn't redefine console.log() You can name it anything you want, like `console.myFunction`. Or just `myFunction` – Ringo Jan 30 '22 at 07:14
0
If you want to show something similar to console.log() inside the HTML, you can just create a div on the page and then put whatever you want to view inside the div. Something like this:
<div id="test"></div>
<script>
const x = "1234";
document.getElementById('test').innerHTML = x;
</script>
This page has some info on how to display the contents of objects and arrays in HTML. https://www.w3schools.com/js/js_object_display.asp

Ringo
- 5,097
- 3
- 31
- 46
-
Thanks. But I did not mean in that way. Let's say I have a function that does order of operations and returns the final result. For example ...calcFunction(7+2x(4-3)) ...my calcFunction goes through many steps and spits out 9 as a return value...What if I want the middle part of the operation...like the parenthesis execution part of the code... I can see the execution steps with console.log. I just cannot grab that and display it in the HTML. But generators might be the answer. – Ebe Gas Jan 30 '22 at 06:32
-
1Sorry, i don't really understand what you're saying. It would be very helpful if you could post some code in your question. – Ringo Jan 30 '22 at 07:10
-