JavaScript, a cornerstone web technology, offers many methods to manipulate the Document Object Model (DOM). One such method is appendChild(), a powerful tool in the Node interface. This blog will explore the appendChild method in JavaScript, its syntax, and usage and provide examples to illustrate its functionality.
The appendChild() method, part of the Node interface in JavaScript, is used to append a node as the last child of a specified parent node. It manipulates the DOM by moving the specified node from its current position to a new one if the node exists simultaneously in the document. Else, it simply appends the node to the specified parent node.
The appendChild() method takes one parameter: the child node to be appended to the specified parent node. The syntax is as follows:
1parentNode.appendChild(childNode); 2
Here, parentNode is the specified parent node to which the child node is to be appended, and childNode is the node to be appended to the parent node.
When the appendChild() method is called, it appends a node to the list of children of a specified parent node. If the given node already exists in the document, appendChild() removes it from its current position and places it at the new position.
1var node = document.getElementById("myList2").lastChild; // Get the last child from the second list 2document.getElementById("myList1").appendChild(node); // Append the last child from the second list to the first list 3
In the above example, the last child of the second list is moved to the end of the first list.
To append a new node, you must first create the node using the document.createElement() method, then append it using the appendChild() method.
1var node = document.createElement("LI"); // Create a new list node 2var textnode = document.createTextNode("Water"); // Create a text node 3node.appendChild(textnode); // Append the text to the list node 4document.getElementById("myList").appendChild(node); // Append the new node to the list 5
In this example, a new list node is created, a new text node is created and appended to the list node, and finally, the new list node is appended.
When appending an existing node using the appendChild() method, the node is removed from its current position and added to the new position in the specified parent node.
1var node = document.getElementById("myList2").childNodes[0]; // Get the first child node of the second list 2document.getElementById("myList1").appendChild(node); // Append the first child node of the second list to the first list 3
In this example, the first child node of the second list is moved to the end of the first list.
The appendChild() method only takes one node at a time. However, to append multiple child nodes, you can use a loop.
1var node, textnode, i, 2 fruits = ['Apple', 'Banana', 'Mango']; 3for (i = 0; i < fruits.length; i++) { 4 node = document.createElement("LI"); // Create a new list node 5 textnode = document.createTextNode(fruits[i]); // Create a text node 6 node.appendChild(textnode); // Append the text to the list node 7 document.getElementById("myList").appendChild(node); // Append the new node to the list 8} 9
In this example, multiple list nodes are created and appended to the list.
The appendChild() method is useful in various scenarios:
While the appendChild() method is incredibly useful, it does have some limitations:
Let's look at a method example where we create a new paragraph element, create a text node, append the text node to the paragraph, and finally append the paragraph as a child node to the document's body.
1var para = document.createElement("p"); // Create a new paragraph element 2var node = document.createTextNode("This is a new paragraph."); // Create a text node 3para.appendChild(node); // Append the text node to the paragraph 4document.body.appendChild(para); // Append the paragraph to the body 5
In this example, the new paragraph element with text is appended as the last child of the body, hence it will appear at the bottom of the webpage.
Let's look at more examples to illustrate the use of appendChild() further.
Appending an existing child node to a new parent node
1var node = document.getElementById("myList1").firstChild; // Get the first child node of the first list 2document.getElementById("myList2").appendChild(node); // Append the first child node of the first list to the second list 3
In this example, the first child node of the first list is moved to the end of the second list.
Appending a cloned node
1var node = document.getElementById("myList1").firstChild; // Get the first child node of the first list 2var clone = node.cloneNode(true); // Clone the first child node 3document.getElementById("myList2").appendChild(clone); // Append the cloned node to the second list 4
In this example, the first child node of the first list is cloned and the cloned node is appended to the second list. The original node remains in its position in the first list.
The appendChild() method in JavaScript is a powerful tool for manipulating the DOM. It allows you to add new elements dynamically, move existing elements, and clone and append elements. However, it does have some limitations, such as the inability to append multiple child nodes at once and the fact that it moves existing nodes rather than creating new ones. Despite these limitations, appendChild() remains a fundamental method for DOM manipulation in JavaScript.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.