sourlemonaid sourlemonaid - 1 year ago 60
HTML Question

configuring my code to append to child instead of innerHTML

I have this code I wrote
Currently it uses innerHTML to write to the page with a forloop. However, I noticed that instead of writing everything. It just writes the last element in the array.

I searched and saw that I have to use the DOM to get this done as innerHTML will ALWAYS destroy instead of replacing. So I wrote this

function print(message) {
var mydiv = document.getElementById("box");

To go inside my code and replace the innerHTML line. However, this doesn't seem to work. Can anybody help me getting it running?

by "this doesn't seem to work" i mean that the code won't append to the child of box. so my text will not be inserted into the element with the 'box' ID.
What i want to happen is just like in my codepen where i used innerHTML but instead of each only the last element in the array showing up, i want them all to show up

okay updated the codepen. seems the code I posted on this page kinda works. but it doesn't seem to go in the right place. if you check the codepen and add some text and scroll down you can see it doesn't fit into place

Answer Source

Change the print function in this way:

function print(message) {
   var html =  document.getElementById("box").innerHTML;
   document.getElementById("box").innerHTML = html + message;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download