Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript DOM Scripting By Example Adding and Removing Names Removing Names

Ross Horak
Ross Horak
20,890 Points

Why not use the .remove() and .closest() methods?

A. Why not just use the .remove() method instead of traversing to the parent element in order to use .removeChild()?

// Using removeChild()
childElement.parentNode.removeChild(childElement);

// Using remove()
childElement.remove()

B. Why not use the .closest('selector') instead of repeating .parentNode to traverse to grandparent? (Especially if the ancestor element is a greater ancestor than a grandparent)

// Using .parentNode()
let grandParent = childElement.parentNode.parentNode;

// Using .closest()
let grandParent = childElement.closest('selector');

1 Answer

Steven Parker
Steven Parker
231,248 Points

The "remove" and "closest" methods are relatively recent developments and don't have the browser support that the older methods do. In particular, neither is supported at all by IE.

But if you're writing code to be used inside a closed private network, or otherwise know that browser compatibility is not going to be an issue for your users, you can start using these now.

But it makes sense that the courses would teach the methods that are most universally compatible.

Ross Horak
Ross Horak
20,890 Points

Makes sense thanks Steven.