LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 15

innerHTML += after adding a listener Answer, explanation & discussion

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

The page contains this HTML:

<div id="box"><button id="go">Go</button></div>

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

main.js
let clicks = 0;
const original = document.getElementById("go");
original.addEventListener("click", () => clicks++);
document.getElementById("box").innerHTML += "<p>Added</p>";
const current = document.getElementById("go");
current.click();
console.log(clicks, current === original);
  • A 1 true
  • B 1 false
  • C 0 true
  • D 0 false Correct
Correct Answer: Option D

innerHTML += re-parses the whole content, so the button is a brand-new element without the listener.

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