Element appendChild() Method - Javascript DOM

Javascript examples for DOM:Element appendChild

Description

The appendChild() method appends a node to current element.

Parameter Values

Parameter TypeDescription
node Node object Required. The node object you want to append

Return Value:

A Node Object, representing the appended node

The following code shows how to Append an item in a list:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<ul id="myList1"><li>Coffee</li><li>Tea</li></ul>
<ul id="myList2"><li>Water</li><li>Milk</li></ul>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//  w  ww .ja  v a  2  s .  c  o  m
    var node = document.getElementById("myList2").lastChild;
    document.getElementById("myList1").appendChild(node);
}
</script>

</body>
</html>

Related Tutorials