LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 3

Live and static lists after adding an item Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

The page contains this HTML:

<ul id="tasks">
  <li class="task done">Read</li>
  <li class="task">Code</li>
  <li class="task done">Test</li>
</ul>

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

main.js
const staticList = document.querySelectorAll(".task");
const liveList = document.getElementsByClassName("task");
const li = document.createElement("li");
li.className = "task";
document.getElementById("tasks").append(li);
console.log(staticList.length, liveList.length);
  • A 4 4
  • B 3 3
  • C 3 4 Correct
  • D 4 3
Correct Answer: Option C

querySelectorAll took a snapshot; getElementsByClassName is live and sees the new item.

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