JavaScript removeChildIn JavaScript, we have learned the creation of nodes where we create several nodes in which one is known as the Parent node or Root node, and all other nodes coming from the parent node are known as Child nodes. Sometimes there may be cases where we need to remove some or all child nodes of the parent node for which we need a method through which we can easily remove a child node. So, in this section, we will discuss and understand the method through which we can remove a child node from its parent node. Also, we will look at some examples that will help us to understand the method well. The removeChild() methodIn order to remove a child node from a parent node, we use the removeChild () method. The removeChild () method is used for removing a child element of a node. Syntax The syntax for using the removeChild () method is: In the syntax, childNode is that element of the node that we want to remove from its parent Node. Thus, as a parameter value, we pass the childNode value that we want to remove. Also, if the specified child node in the childNode is not part of the specified parentNode, then an exception will be thrown by the method. However, if the specified childNode is present in its parentNode, then the removeChild () method will remove and return that childNode from the DOM, but it does not remove that child from memory and keep it in there. Thus, if we don't want to keep that specified childNode in the memory also and want to remove it from memory, use the below syntax: In this syntax, we could notice that we have not stored the removed childNode in any variable (as in the above syntax), which means the removed element will not get stored anywhere in the memory too. However, it will still exist in the memory until the JS garbage collector wrecks it. Now, let's see some examples that will help us to understand the concept better. JavaScript removeChild() ExamplesLet's have some examples to understand the practical implementation of the removeChild () method: Example 1: Test it NowThe output of the above code is shown below: Code Explanantion
Example 2: Test it NowThe output of the above code is shown below: In this example, we have removed the last child element from the list. In the code:
Note: The removeChild () method is for removing only one child element from the root element, but if we want to remove all child elements from the root element, there are other methods also available through which we can remove all the child elements of a root node.Remove all child nodesIn order to remove all child methods of a root node, there are the following ways through which we can do so: Method 1 Follow the below steps for removing all the child nodes of an element:
Method 2 The other method is to use innerHTML property of the parent node by setting innerHTML = "" i.e. an empty string. The use of the method is shown below: However, innerHTML is a method to remove all child nodes of an element, but the method is not preferred to be used. So, these are the methods through which we can remove one or all child nodes of an element. |