Skip to content Skip to sidebar Skip to footer

Reverse The Order Of Elements Added To Dom With Javascript

I am making a game in JavaScript, and need an event log. If i attack, it tells me if i hit or i miss. Here is my code: function eventlogshow (text){ var para = document.createE

Solution 1:

Prepend the child element instead. Since there is no prependChild() function, you need to "insert it before the first child":

functioneventlogshow (text){
    var para = document.createElement("p");
    var node = document.createTextNode(text);

    para.appendChild(node);

    var element = document.getElementById("eventlog");
    element.insertBefore(para, element.firstChild);
}

A similar question has been asked here: How to set DOM element as first child?.

Read more about Node.firstChild and Node.insertBefore()

Solution 2:

appendChild adds a node as a last child. You want to insert before the first node:

element.insertBefore(para, element.childNodes[0]);

Post a Comment for "Reverse The Order Of Elements Added To Dom With Javascript"