First off, parent.appendChild(node)
takes a DOM node as an argument. If that DOM node is already in the document, it will be moved to the location specified.
Second off, document.getElementById(id)
retrieves a DOM node. node.value
gets the text from a form field. You cannot use that text as an argument to .appendChild()
. If you want to insert a copy of that text somewhere in the DOM, then you would need to create a new DOM node, put the text that you obtained from node.value into that new DOM node and then parent.appendChild(newNode)
that new DOM node.
If you can clarify exactly what you want the end result to look like (show what the HTML would look like after hitting the Add button), then we could offer an exact code solution for you. As your question stands now, it is not clear what you want the end result to be.
As a simple example, here's some code that reads from an input field and inserts that text into the DOM every time you press a button:
<input id="newComment" type="text" /><br>
<button onclick="add()">Add</button><br><br>
<div id="comments"></div>
<script>
function add() {
var newText = document.getElementById("newComment").value;
if (newText) {
var paragraph = document.createElement("p");
paragraph.textContent = newText;
document.getElementById("comments").appendChild(paragraph);
}
}
</script>
Working demo: http://jsfiddle.net/jfriend00/T7dtD/