LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 9

stopPropagation in the middle Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 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 = [];
document.getElementById("outer").addEventListener("click", () => order.push("outer"));
document.getElementById("inner").addEventListener("click", (e) => {
  order.push("inner");
  e.stopPropagation();
});
document.getElementById("inner").addEventListener("click", () => order.push("inner 2"));
document.getElementById("btn").click();
console.log(order.join(", "));
  • A inner
  • B inner, inner 2 Correct
  • C inner, inner 2, outer
  • D inner, outer
Correct Answer: Option B

stopPropagation stops the event from reaching outer, but other listeners on the same element still run (that would need stopImmediatePropagation).

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