Question 19
Back to Exercise
The page contains this HTML:
<template id="row"><li class="topic"></li></template><ul id="topics"></ul>What does the following JavaScript code print when it runs on this page?
const tpl = document.getElementById("row");
const list = document.getElementById("topics");
for (const name of ["Closures", "Promises", "DOM"]) {
const li = tpl.content.cloneNode(true).querySelector("li");
li.textContent = name;
list.append(li);
}
console.log(list.children.length, tpl.content.children.length, document.querySelectorAll(".topic").length);
console.log(list.lastElementChild.outerHTML);
- A 3 0 3 <li class="topic">DOM</li>
- B 3 1 3 <li class="topic">DOM</li> Correct
- C 3 1 4 <li class="topic">DOM</li>
- D 1 1 1 <li class="topic">Closures</li>
Correct Answer: Option B
Template content is an inert fragment that is not part of the page, so document.querySelectorAll does not see it; cloning leaves the template unchanged.
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!