LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 8

A capturing listener Answer, explanation & discussion

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

The capturing listener on outer runs on the way down, before the target; the others run while bubbling.

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