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
Heather Gray6,225 Points
Why wouldn't you choose the <ul> ancestor instead of the <div>?
In the Event Bubbling and Delegation video, the instructor is showing why it's helpful to choose an ancestor to put an event listener on rather than looping through each li item to put a listener on each one.
Example code I made up that is similar to video:
In the above example, the instructor chose to use the div as the element to put the event listener on rather than the ul. I'm trying to understand why. Because this would mean that the event listener would trigger events for the <p> element too which isn't actually one of the list items.
You're right, the
<ul> would be a better (closer) ancestor to attach the event to. I think the reason Gil didn't is because the ul is "naked" and doesn't have any class or id to give it enough specificity to select in the JS -- also, in the JS, Gil has already created a variable for the
.list element, so he just used what's already ready to go.
If you were optimizing your site for performance, you would be right to maybe add a class to the ul, select it in your JS, and then put the click handler on it rather than the