[{"id":"domapisintermediate-001","courseId":"dom-apis-intermediate","domain":"dom-tree","topic":"childNodes vs children","prompt":"In markup with a line break between each <li> tag, what does list.childNodes contain compared with list.children?","hint":"Look at which node types each of the two collections is willing to include.","explain":"The browser keeps every piece of the source as a node, so a child list can contain more than the tags you wrote. Filtering to one kind of node is a separate choice.","source":"https://developer.mozilla.org/en-US/docs/Web/API/Node/childNodes","interaction":{"type":"single"},"options":[{"id":"a","text":"childNodes includes whitespace text nodes; children holds only elements","why":"childNodes returns every child node, text and comments included, while Element.children keeps only the elements."},{"id":"b","text":"Both return the same items, because the parser strips whitespace from the DOM","whyNot":"The browser inserts whitespace as text nodes, and childNodes exposes them rather than removing them."},{"id":"c","text":"children includes text nodes; childNodes holds only element nodes","whyNot":"This swaps the two. childNodes is the broader list; children is the element-only one."},{"id":"d","text":"childNodes returns an array of strings, one per tag in the markup","whyNot":"The items are node objects, not strings, so you read their data through their properties."}],"answer":"a","explanation":"Node.childNodes returns every child node, text and comments included; to get only elements, use Element.children instead.","concept":"Node.childNodes returns every child node, text and comments included; to get only elements, use Element.children instead.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","code":"<ul id=\"list\">\n  <li>One</li>\n  <li>Two</li>\n</ul>","takeaways":["childNodes counts whitespace text nodes; use children when you only want elements."]},{"id":"domapisintermediate-002","courseId":"dom-apis-intermediate","domain":"dom-tree","topic":"DocumentFragment as off-document container","prompt":"A script builds five <li> elements inside a DocumentFragment, then calls ul.append(fragment). Which TWO statements about the fragment and the list are correct?","hint":"A fragment is not part of the page until something inserts it.","explain":"A fragment is a holding area that sits outside the document tree, so work done inside it stays off the page. Consider what an insertion method does to the nodes it receives.","source":"https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"The fragment is left empty, because its nodes have moved into the list","why":"Inserting a fragment moves its nodes into the target and leaves an empty fragment behind."},{"id":"b","text":"Adding nodes to the fragment does not by itself change the document","why":"Changes made to a fragment don't affect the document, because the fragment is not part of the active tree."},{"id":"c","text":"The fragment element stays in the list as a wrapper around the five items","whyNot":"A fragment is never inserted as a node; only its children move into the list."},{"id":"d","text":"The fragment keeps its five nodes, and the list receives copies of them","whyNot":"Insertion moves the nodes rather than copying them, which is why the fragment ends up empty."},{"id":"e","text":"Building inside a fragment is always faster than appending to the list in a loop","whyNot":"MDN notes the speed benefit is often overstated; in some engines a fragment is slower than a loop."},{"id":"f","text":"Appending the same fragment again adds the five items a second time","whyNot":"The first insert emptied the fragment, so a second append has nothing left to add."}],"answer":"ab","explanation":"A DocumentFragment is a lightweight container outside the active tree; changes to it don't affect the document, and inserting it moves its nodes out and leaves it empty.","concept":"A DocumentFragment is a lightweight container outside the active tree; changes to it don't affect the document, and inserting it moves its nodes out and leaves it empty.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","code":"const ul = document.querySelector(\"ul\");\nconst frag = new DocumentFragment();\nfor (const name of names) {\n  const li = document.createElement(\"li\");\n  li.textContent = name;\n  frag.append(li);\n}\nul.append(frag);","takeaways":["Inserting a fragment moves its children into the page and leaves the fragment empty."]},{"id":"domapisintermediate-003","courseId":"dom-apis-intermediate","domain":"dom-tree","topic":"template element contents","prompt":"In a browser, looping over template.childNodes visits every tag written between a <template> element's opening and closing tags.","hint":"Think about where a template's markup is stored, not how it renders.","explain":"An element's child list normally mirrors the markup inside it. Some elements keep that markup in a separate container with its own property. Consider where a template keeps its markup.","source":"https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/template","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"A template's childNodes is always empty; its nested markup is reached only through the content property."},{"id":"b","text":"False","why":"The nested markup goes into a separate DocumentFragment exposed by content, so the template's childNodes list stays empty."}],"answer":"b","explanation":"A template element's childNodes is always empty; its nested markup is kept in a DocumentFragment exposed through the read-only content property.","concept":"A template element's childNodes is always empty; its nested markup is kept in a DocumentFragment exposed through the read-only content property.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Reach a template's markup through its content property, not its childNodes."]},{"id":"domapisintermediate-004","courseId":"dom-apis-intermediate","domain":"dom-tree","topic":"cloneNode shallow vs deep","prompt":"A page has a <ul> with five <li> children. A script runs list.cloneNode() with no argument and appends the result to the body. What appears on the page?","hint":"Look at the optional argument, and what its default value is.","explain":"Copying a node can take just the node itself or also everything inside it, and a flag decides which. Think about what a copy of the node alone keeps.","source":"https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode","interaction":{"type":"single"},"options":[{"id":"a","text":"An empty ul with the same attributes, and no list items","why":"Without the deep argument, the node and its attributes are copied, and its subtree is not."},{"id":"b","text":"A ul that holds all five li items with their text","whyNot":"Copying the subtree needs the deep argument set to true, which this call leaves out."},{"id":"c","text":"A ul holding the five li items, but without their text","whyNot":"The text lives in child nodes, so a copy that skips the subtree loses it along with the items."},{"id":"d","text":"A ul whose li items are the originals, moved out of the page","whyNot":"cloneNode() returns a new node and leaves the original in place; nothing is moved."}],"answer":"a","explanation":"cloneNode() copies the node and its attributes; the subtree is copied only when the deep argument is true, and it defaults to false.","concept":"cloneNode() copies the node and its attributes; the subtree is copied only when the deep argument is true, and it defaults to false.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","code":"const list = document.querySelector(\"ul\");\nconst copy = list.cloneNode();\ndocument.body.append(copy);","takeaways":["cloneNode() with no argument copies only the element, not the children inside it."]},{"id":"domapisintermediate-005","courseId":"dom-apis-intermediate","domain":"dom-tree","topic":"Node.contains","prompt":"A script calls document.body.contains(document.body), passing the body itself as the argument. What does the call return?","hint":"Think about whether the method counts the node itself.","explain":"contains() asks whether one node sits inside another, descendants included. Consider how the method treats a node that is compared with itself.","source":"https://developer.mozilla.org/en-US/docs/Web/API/Node/contains","interaction":{"type":"single"},"options":[{"id":"a","text":"true, because a node counts as contained in itself","why":"contains() is inclusive, so the node itself counts; that is why body.contains(body) returns true."},{"id":"b","text":"false, because a node is never inside its own subtree","whyNot":"That is the exclusive reading; contains() counts the node itself, so this call does not return false."},{"id":"c","text":"null, because there is no separate node to compare with","whyNot":"contains() returns a boolean, so null is never a possible return value."},{"id":"d","text":"TypeError, because a node cannot take itself as an argument","whyNot":"The argument is an ordinary node, so the call runs without error."}],"answer":"a","explanation":"Node.contains() is inclusive: it returns true when the argument is the node itself or any of its descendants.","concept":"Node.contains() is inclusive: it returns true when the argument is the node itself or any of its descendants.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","code":"const inside = document.body\n  .contains(document.body);","takeaways":["contains() counts a node as inside itself; compare with === if you want descendants only."]}]