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()

Please help, I don't understand why it doesn't work if i put the code not below the $('#posted-pets').append($newPet);

This code work

// Attach the new element to the page $('#posted-pets').append($newPet);

$('.close').on('click', function() { $(this).parent().remove(); });

2 Answers

jared eiseman
jared eiseman
29,023 Points

If I'm understanding your question correctly in that you're wondering why the click event binding doesn't work if you put it above the line that appends the pet to the page. I'm also making an assumption about the rest of your code, I might be able to answer better if you post the rest of it.

When you bind a click event to an element, the element has to exist. It gains that event handler as part of it's properties. If the element doesn't exist on the page yet, when the .click() method tries to find an element to bind to, none such exists, so It will never gain that property so to speak.

thanks Jared, got it.