Last updated: Mar 7, 2024
Reading time·4 min
The JavaScript Uncaught DOMException: Failed to execute 'removeChild' on
'Node' occurs when the element you pass to the removeChild()
method is not a
direct child of the node on which you called the method.
To solve the error, make sure to pass a direct child to the removeChild()
method.
Here is the complete error message.
Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
NOTE: If you got the error in React.js, click on the following subheading:
Here is an example of how the error occurs.
This is my index.html
file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="parent"> Parent <div id="child"> Child <div id="nested-child">Nested Child</div> </div> </div> <script type="module" src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const parent = document.getElementById('parent'); const nestedChild = document.getElementById('nested-child'); // ⛔️ Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node. // ⛔️ Uncaught DOMException: Node.removeChild: The node to be removed is not a child of this node parent.removeChild(nestedChild);
We called the removeChild() method with the nested child element and not with the direct child which caused the error.
The method cannot be called with a descendant, it has to be called with a direct child of the element.
removeChild()
One way to solve the error is to pass a direct child of the element to the
removeChild()
method.
const parent = document.getElementById('parent'); const child = document.getElementById('child'); parent.removeChild(child);
If I open the page in my browser, I can see that the child element has been removed.
An alternative approach to solving the error is to remove the wrapper element.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="parent"> Parent <div id="nested-child">Nested Child</div> </div> <script type="module" src="index.js"></script> </body> </html>
Notice that I removed the child
element, so the nested-child
element is not
a direct child of the parent
element.
const parent = document.getElementById('parent'); const nestedChild = document.getElementById('nested-child'); parent.removeChild(nestedChild);
Now, calling the removeChild
method with the element that has an id
of
nested-child
works.
I have also written 2 relevant articles:
If you got the error in a React.js application, replace the React fragment with
a div
element.
Replace the following.
// Replace this <React.Fragment>Example</React.Fragment> // or this <>Example</>
With the following.
<div>Example</div>
The error often occurs in a React.js application when using third-party
libraries that use the removeChild()
method under the hood.
React fragments are used when you need to group a list of children without adding extra nodes to the DOM.
However, in some cases, an extra node element is needed and serves as a parent
element enabling these third-party libraries to use the removeChild
method.
remove()
method instead of removeChild()
If the error persists, you might want to use the remove()
method to remove the
given element.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="parent"> Parent <div id="child"> Child <div id="nested-child">Nested Child</div> </div> </div> <script type="module" src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const nestedChild = document.getElementById('nested-child'); nestedChild.remove();
If I load the page in my browser, I can see that the element has been removed.
I have written a detailed guide on how to remove a DOM element by ID.
The remove()
method removes the DOM element on which it is called.
If you need to remove the parent element of a Node, check out the following article.
You can learn more about the related topics by checking out the following tutorials: