0
So I was trying to create a rudimentary system of "posts" with a history where they would all be stored, but I realized that there was a problem that I couldn’t solve, which was the fact that the most recent posts were created below the oldest.
In this case, the idea was that each new "post" would override the previous one so that the latest one would always be at the top of the history.
Example of the output displayed in the history
In the case of codes all I did was:
CSS
div#newDivSpace {
border: 1px solid #aaa;
min-height: 30px;
width: 240px;
margin-top: 10px;
margin-left: 5px;
}
.post {
margin-bottom: 1px;
padding: 5px;
border-bottom: 1px solid #aaa;
}
HTML
<form action="#">
<input type="text" id="userText">
<button type="button" id="divGenerator" onclick="newDiv()">Criar div</button>
</form>
<div id="newDivSpace"></div>
Javascript
var input = document.getElementById("userText");
var button = document.getElementById("divGenerator");
var space = document.getElementById("newDivSpace");
var post = document.getElementsByClassName("post");
function newDiv() {
var newDiv = document.createElement("div");
var userInput = input.value;
if (userInput != "") {
newDiv.innerHTML = userInput;
newDiv.classList.add("post");
space.appendChild(newDiv);
input.value = "";
}
If it is possible to help with the implementation of a system where the latest message is always at the top of the history sera great help.
space.insertBefore(newDiv,someParentObject.firstchild)
– Alvaro Alves