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 JavaScript and the DOM (Retiring) Responding to User Interaction The Event Object

chettigari naveen
chettigari naveen
1,198 Points

why not to target the UL element instead of DIV, so we can stop using IF block

please provide reason for using DIV when we have list items parent UL.

2 Answers

Steven Parker
Steven Parker
231,236 Points

You would still want an "if" to filter on specific elements even if the delegated handler were placed closer, since it would also get triggered by events on children of the list items.

Another good reason to apply the filter is to prevent unexpected behavior changes from future additions to the HTML.

Ivan Kazakov
PLUS
Ivan Kazakov
Courses Plus Student 43,317 Points

Still adding an event listener to the <ul> makes sense to me, even though it doesn't eliminate the necessity to check for the target. Makes less event handling in the DOM, since `<p>, <input> and <button> aren't included in that case.

Steven Parker
Steven Parker
231,236 Points

It is certainly good practice to place the delegated hander on the first common ancestor of the elements to be covered. But "less event handling" isn't really a concern, considering that events caused by user interaction will be extremely infrequent from the perspective of computing time.