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 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
210,872 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,749 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.