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 Introduction to jQuery DOM Traversal parent()

So the parent node is the newly created div right? So therefore you have to do parent().parent() to remove the entire

newly created section element? I'm asking because otherwise if you create like 10 new pets and remove them, you'll still have extra section elements in the DOM.

1 Answer

Yes that is correct. Doing $(this).parent().remove(); will remove only the DIV element within the section leaving an empty section behind. Doing $(this).parent().parent()remove(); will remove the entire section completely.

Alternatively you can also use $(this).parents('section').remove(); and use a selector as an argument to parents to traverse up the DOM until a match is found so you wont need that extra .parent() method call.