LearnFrenzy
Sign in Take a free test

DOM Manipulation · Find the Error · Question 12

Bug: delete removes "undefined" Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

The page contains this HTML:

<ul id="list"><li data-id="7"><span>Revise arrays</span></li></ul>

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
document.getElementById("list").addEventListener("click", (e) => {
  console.log("delete item", e.target.dataset.id);
});
document.querySelector("#list span").click();     // the user clicks the text
  • A dataset.id must be dataset.ID
  • B e.target is the inner <span>, which has no data-id – use e.target.closest("li").dataset.id Correct
  • C The listener should be on the span
  • D click() does not bubble
Correct Answer: Option B

target is the deepest element clicked. closest("li") walks up to the element that carries the data.

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