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 trialEdsel Talento
16,922 PointsPlease 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
29,023 PointsIf 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.
Edsel Talento
16,922 Pointsthanks Jared, got it.