LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 7

Bubbling order Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

The page contains this HTML:

<section id="outer">
  <div id="inner"><button id="btn">Click</button></div>
</section>

What does the following JavaScript code print when it runs on this page?

main.js
const order = [];
for (const id of ["outer", "inner", "btn"]) {
  document.getElementById(id).addEventListener("click", () => order.push(id));
}
document.getElementById("btn").click();
console.log(order.join(" > "));
  • A outer > inner > btn
  • B btn > inner > outer Correct
  • C btn
  • D inner > btn > outer
Correct Answer: Option B

Bubbling listeners run from the target up through its ancestors, whatever order they were added in.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links