JavaScript JavaScript and the DOM Traversing the DOM Using parentNode to Traverse Up the DOM

So removeChild is the only way to remove a node? It seems like extra work to have to get parent and then delete child

It seems a little unnecessary to have to traverse the DOM to remove the child by first getting the parent. Are there other, more direct, ways to accomplish this? Or is this solely to demonstrate how to traverse the DOM?

Thank you :)

1 Answer

Steven Parker
Steven Parker
173,522 Points

Objects that qualify as a "ChildNode" also have a direct "remove" method on most browsers, but MDN still considers this method "experimental" and it is not supported on iOs Safari or IE.

Using official and fully-supported methods (like "removeChild") is strongly recommended for any production code.