Created
July 20, 2021 08:21
-
-
Save dadinugroho/945a2b9b8dee4306be1a61140484cd33 to your computer and use it in GitHub Desktop.
Comment List and Form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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