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

Amandeep Pasricha
Amandeep Pasricha
14,932 Points

Need clarification on event bubbling

I wanted to clarify this question to better understand eventBubbling.

An event handler tells you tells the browser what to do once you have heard an event. That's great, but how does the ancestor know to apply the event to its children, if the event handler has been applied to the ancestor instead?

So basically, an event occurs on a child element, that information goes up, the parent receives this information due to event bubbling, and then it applies what was in the function to its children simply BECAUSE it had received the event through event bubbling? So the parent knew the ORIGIN of where the event occurred because of event bubbling? Am I correct?

1 Answer

Brendan Whiting
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,702 Points

A parent/ancestor element doesn't know how to apply that element to its children unless you explicitly give it instructions, and those instructions are defined in the function you pass as the callback in the event handler.

<div id="parent">
  <div>child 1</div>
  <div>child 2</div>
  <div>child 3</div>
const parent = document.getElementById("parent");
parent.addEventListener("click", function() {
  const children = parent.querySelectorAll("div");  
  for (child of children) {
    child.style.color = "red";

In this example, whichever child you click, the event will bubble up to the parent. And the parents instructions is to set ALL the children's text color to red, regardless of which one was clicked, because those are the instructions that I gave it in my event handler.