LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 16

dispatchEvent and preventDefault Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 16 Back to Exercise

The page contains this HTML:

<a id="link" href="/exam">Start exam</a>

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

main.js
const link = document.getElementById("link");
link.addEventListener("click", (e) => e.preventDefault());
const a = link.dispatchEvent(new Event("click", { cancelable: true }));
const b = link.dispatchEvent(new Event("click"));
console.log(a, b);
  • A true true
  • B false false
  • C true false
  • D false true Correct
Correct Answer: Option D

dispatchEvent returns false only if the event was cancelable and a listener called preventDefault; on a non-cancelable event preventDefault does nothing.

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