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 trialDou Sun
2,754 PointsI have a question about the final part
so my code is like this
// 3. Add the class "strike" to the restaurant name each time it's double clicked. Use event delegation on the ul element to listen for a click event on the ul's list items
$('li').on('dblclick', function(){ $(this).toggleClass('strike'); });
it works just for the 4 'li' elements that already existed in the HTML file, but not for the new restaurants (new 'li' element) that I added in the second part. why?
1 Answer
Steven Parker
231,248 PointsThose listeners are attached directly to the original list items. New ones won't have listeners unless you attach them specifically to each one. That's why the instructions say "Use event delegation on the ul element ...".
A delegated handler will work with both the original items and any that are created later, since the event will propagate ("bubble up") to the parent ul element.