LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 17

A CustomEvent that does not bubble Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 17 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 btn = document.getElementById("btn");
document.getElementById("outer").addEventListener("score", (e) => console.log("outer got", e.detail.points));
btn.addEventListener("score", (e) => console.log("button got", e.detail.points));
btn.dispatchEvent(new CustomEvent("score", { detail: { points: 5 } }));
btn.dispatchEvent(new CustomEvent("score", { detail: { points: 10 }, bubbles: true }));
  • A button got 5 button got 10 outer got 10 Correct
  • B button got 5 outer got 5 button got 10 outer got 10
  • C button got 5 button got 10
  • D outer got 10 button got 5 button got 10
Correct Answer: Option A

Custom events do not bubble unless you pass bubbles: true.

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