JavaScript JavaScript and the DOM 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
180,120 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
Ivan Kazakov
43,310 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
180,120 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.