I'm currently working on implementing a commenting feature for my website. The goal is to display new comments above the existing comment section when users submit their feedback. However, I'm facing challenges in dynamically updating the HTML with the new comments using JavaScript. My aim is to maintain consistency in the formatting of the new comment compared to the initial comments displayed. How can I achieve this? Apologies if my explanation is not clear enough :/
var post= document.getElementById("post");
post.addEventListener("click",function(e){
e.preventDefault();
console.log('comment button pressed');
var userComment=document.getElementById("comment-box").value;
console.log(userComment)
var text= document.createTextNode(userComment);
console.log(text)
var newComment = document.getElementById("new-comment");
console.log(newComment)
newComment.appendChild(userComment)
})
.comment-box,
.post-comment .list{
background-color: white;
border-radius: 5px;
box-shadow: 0 2px 2px black;
}
.comment-section{
width: 100%;
height: auto;
margin: 0 auto;
}
.post-comment .list{
width: 100%;
margin-bottom: 12px;
}
.post-comment .list .user{
display: flex;
padding: 8px;
overflow: hidden;
}
.post-comment .list .user img{
height: 38px;
width: 38px;
margin-right: 10px;
border-radius: 50%;
}
.comment-section .name{
text-transform: uppercase;
}
.post-comment .list .day{
font-size: 12px;
}
.post-comment{
padding: 0 0 15px 58px
}
#comment-box{
border:none;
border-radius: 5px;
}
.comment-box .user{
display: flex;
width: min-content;
}
.comment-box .image img{
width:24px;
height: 24px;
margin-right: 10px;
border-radius: 50%;
}
.comment-box textarea{
height: 50px;
width: 1 ;
background-color: white;
margin: 10px 0;
padding: 10px;
border-radius: 5px;
box-shadow: 0 0 0 1px black;
}
<div class="post-comment">
<div class="list">
<div class="user">
<div class="user-image"><img src="./images/ok.webp"></div>
<div class="user-name">
<div class="name">TOM</div>
<div class="day">100 days ago</div>
</div>
</div>
<div class="comment">LOREM IPSUN DABUN VUB</div>
</div>
<div id="new-comment"> </div>
<div class="comment-box">
<div class="user">
<div class="user-image"><img src="./images/OK.webp"></div>
<form>
<textarea name="comment" placeholder="YOUR MESSAGE" id="comment-box"></textarea>
<button id="post">Comment</button>
</form>
</div>
</div>
</div>
</div>