Remove the first <li> element from a list:
Before removing:
After removing:
More "Try it Yourself" examples below.
The removeChild() method removes a specified child node of the specified element.
Returns the removed node as a Node object, or null if the node does not exist.
Note: The removed child node is no longer part of the DOM. However, with the reference returned by this method, it is possible to insert the removed child to an element at a later time (See "More Examples").
Tip: Use the appendChild() or insertBefore() method to insert the removed node into the same document. To insert it to another document, use the document.adoptNode() or document.importNode() method.
Method | |||||
---|---|---|---|---|---|
removeChild() | Yes | Yes | Yes | Yes | Yes |
Parameter | Type | Description |
---|---|---|
node | Node object | Required. The node object you want to remove |
Return Value: | A Node object, representing the removed node, or null if the node does not exist |
---|---|
DOM Version | Core Level 1 Node Object |
Find out if a list has any child nodes. If so, remove its first child node (index 0):
Before removing:
Before removing:
Remove all child nodes of a list:
Before removing:
After removing:
Remove a <li> element with id="myLI" from its parent element (without specifying its parent node):
Before removing:
After removing:
Remove a <li> element from its parent, and insert it again:
Remove a <span> element from its parent and insert it to an <h1> element in another document: