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 Event Bubbling and Delegation

Pranav Mathur
Pranav Mathur
3,419 Points

Bubbling works from bottom of DOM tree upto the top, then how does selecting parent .listDIV apply to all list elements?

In Bubbling, acting on a child element, affects the parent. How does acting on parent Div element affecting all list items work out?

2 Answers

Steven Parker
Steven Parker
229,018 Points

It's the other way around, acting on the li's affects the div.

In the code, the parent listDiv is selected for attaching the event handlers. That way, when you act on one of the list items inside it (the "children"), that event will bubble up to the parent listDiv and trigger the event handler.

So because of "bubbling", the action occurs in the children, but the event is handled at the parent.

Does that make it clear?

ywang04
ywang04
6,762 Points

For example, if we set a click event handler on the body, our callback will trigger whenever any of its children are clicked. due to event bubbling. So if any of the list items, the paragraph, or headline elements are clicked, the callback will trigger in this example. This has been mentioned in this video.