Skip to content

Instantly share code, notes, and snippets.

@dadinugroho
Created July 20, 2021 08:21
Show Gist options
  • Save dadinugroho/945a2b9b8dee4306be1a61140484cd33 to your computer and use it in GitHub Desktop.
Save dadinugroho/945a2b9b8dee4306be1a61140484cd33 to your computer and use it in GitHub Desktop.
Comment List and Form
function addComment() {
const commentContent = document.getElementById('comment').value;
if (commentContent) {
const list = document.getElementById('commentList');
const newComment = document.createElement('li');
newComment.appendChild(document.createTextNode(commentContent));
list.appendChild(newComment);
}
document.getElementById('comment').value = '';
}
function setup() {
const postButton = document.getElementById('postComment');
postButton.addEventListener('click', function(event) {
addComment();
});
}
// Example case.
document.body.innerHTML = `
<ul id='commentList'>
</ul>
<form>
<input type='text' id='comment'/>
<input type='button' id='postComment' value='Post'/>
</form>`;
setup();
document.getElementById("comment").value = "test";
document.getElementById("postComment").click();
console.log(document.body.innerHTML);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment