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?
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!