Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
210,070 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
210,070 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.