Explore Bato Web Agency Webflow Certified Partner Profile Explore Bato Web Agency Webflow Certified Partner Profile

Failed to execute ‘removeChild’ on ‘Node’: parameter 1 is not a child of this node — why?

Illustration without a description Illustration without a description

removeChild() can remove only a direct child of the node it is called on. The target may still exist in the document while belonging to a different parent. It can also have moved or been removed by an earlier update.

 

Inspect parentNode immediately before removal. If you own the node and simply need it detached, child.remove() may fit. If the operation should affect only one list, keep the parent check so a moved item is not removed from another container. For framework-rendered content, change the state that owns it.

 

For a node your code owns, child.remove() can detach it from its current parent. Keep the parent check when the operation should affect only one list. A node that moved to another list should stay there.

 

const parent = document.querySelector('#list'); const child = document.querySelector('#item'); if (parent && child && child.parentNode === parent) parent.removeChild(child);

 

Check node.parentNode just before removal and compare it with the parent on which removeChild was called.