[{"id":"domapisbeginner-001","courseId":"dom-apis-beginner","domain":"dom-tree","topic":"Document and element nodes","prompt":"A colleague writes: 'In the DOM, a node and an element are the same thing, so I can use either word for any object in the tree.' Which statement corrects this?","hint":"Think about the kinds of objects that live inside a document, not just tags.","explain":"The DOM is a tree made of objects, and the tree holds more than the tags you write in markup. People often call every part an element, which blurs a more formal distinction between kinds of parts.","source":"https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction","interaction":{"type":"single"},"options":[{"id":"a","text":"Every node is an element; node is just a formal synonym.","whyNot":"Element is one kind of node. Text and attribute objects are nodes that are not elements."},{"id":"b","text":"Strictly speaking, not every node is an element.","why":"Text and attribute objects are nodes too, so the two words are not interchangeable."},{"id":"c","text":"Attributes are plain strings and are never nodes in the DOM.","whyNot":"Attributes are nodes in the DOM, just like elements are."},{"id":"d","text":"Only tags written in markup count as nodes; text is not a node.","whyNot":"Every object located within a document is a node of some kind, text included."}],"answer":"b","explanation":"Every object in a document is a node. Element is one kind of node; text and attribute objects are nodes too, so element is the narrower word.","concept":"Every object in a document is a node. Element is one kind of node; text and attribute objects are nodes too, so element is the narrower word.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Element is a narrower word than node; text and attribute objects are nodes too."]},{"id":"domapisbeginner-002","courseId":"dom-apis-beginner","domain":"dom-tree","topic":"querySelector returns the first match","prompt":"A page has three paragraphs that all have the class intro. A script runs document.querySelector('.intro'). Which TWO statements about its result are true?","hint":"Think about how many results the method hands back, and what it gives when nothing matches.","explain":"A query can return one match or a whole list, and an empty result can be signaled in more than one way. The search walks the markup in a fixed order, so which match comes back depends on where elements sit.","source":"https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"It returns an array holding all three matching paragraphs.","whyNot":"Returning every match is the job of querySelectorAll(), not this method."},{"id":"b","text":"It returns the first matching element in the document, not all three.","why":"It returns one Element: the first match the traversal finds."},{"id":"c","text":"It returns the last matching paragraph, because it searches from the end.","whyNot":"The traversal starts at the top of the markup, so the first match wins."},{"id":"d","text":"If nothing matches, it returns null instead of throwing an error.","why":"With no match it returns null; an exception is raised only for invalid selector syntax."},{"id":"e","text":"It throws an exception whenever no element matches the selector.","whyNot":"No match gives null; an exception is raised only when the selector itself is invalid."},{"id":"f","text":"It returns the text of the first paragraph instead of the element.","whyNot":"It returns the Element object itself, not its text."}],"answer":"bd","explanation":"querySelector() returns the first Element that matches, found by a depth-first walk of the markup, or null when no element matches.","concept":"querySelector() returns the first Element that matches, found by a depth-first walk of the markup, or null when no element matches.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["querySelector() returns one element or null; querySelectorAll() returns every match."]},{"id":"domapisbeginner-003","courseId":"dom-apis-beginner","domain":"dom-tree","topic":"Live HTMLCollection","prompt":"A script gets an HTMLCollection from the page, then adds a new matching element to the document. Statement: the collection stays fixed, so it never includes that new element.","hint":"Think about whether the list stays tied to the page or is detached from it.","explain":"A list of elements from a DOM call can be a frozen copy or a view that follows later edits. Which one you hold decides whether looping over it is safe while nodes change.","source":"https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"Not so: an HTMLCollection is live and updates automatically when the document changes."},{"id":"b","text":"False","why":"Right: the collection is live, so the newly added matching element shows up in it."}],"answer":"b","explanation":"An HTMLCollection is live: the browser keeps it updated as the document changes, so copy it with Array.from before looping while adding or removing nodes.","concept":"An HTMLCollection is live: the browser keeps it updated as the document changes, so copy it with Array.from before looping while adding or removing nodes.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Live collections change under you; copy them with Array.from before looping over edits."]},{"id":"domapisbeginner-004","courseId":"dom-apis-beginner","domain":"dom-tree","topic":"innerHTML parses markup","prompt":"A chat app shows each user's nickname in a div. Users may type characters such as <b>, and the page must show them exactly as typed, not as markup. Which assignment does this?","hint":"Look for a property that treats the string as characters, not markup.","explain":"Some properties read and write markup, so a string assigned to them can become elements. Others treat a string as plain characters. Typed user input calls for the second kind.","source":"https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML","interaction":{"type":"single"},"options":[{"id":"a","text":"div.innerHTML = nickname;","whyNot":"innerHTML parses the string as HTML, so tags become elements and injected code can run."},{"id":"b","text":"div.textContent = nickname;","why":"textContent sets plain text, so angle brackets are shown literally and never parsed as HTML."},{"id":"c","text":"div.outerHTML = nickname;","whyNot":"outerHTML replaces the div itself, so the container is lost along with its old content."},{"id":"d","text":"div.insertAdjacentHTML('beforeend', nickname);","whyNot":"This also parses its string as HTML, so the markup would still be interpreted."}],"answer":"b","explanation":"innerHTML parses its string as HTML and replaces the element's children, so injected markup runs. textContent sets plain text that is never parsed as HTML.","concept":"innerHTML parses its string as HTML and replaces the element's children, so injected markup runs. textContent sets plain text that is never parsed as HTML.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["Use textContent for user text; innerHTML turns the string into markup and risks XSS."]},{"id":"domapisbeginner-005","courseId":"dom-apis-beginner","domain":"dom-tree","topic":"Creating elements with createElement","prompt":"A basic example builds a greeting div and places it on the page before the element with the id div1. Put its four main steps in the order the example runs them.","hint":"Some steps can only run once an object from an earlier step exists.","explain":"Building part of a page in code means a chain of operations on objects the page cannot see yet. Some operations need an object that an earlier one made.","source":"https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement","interaction":{"type":"sort","items":[{"text":"Create a new div element","why":"The div must exist before it can hold content or be placed on the page."},{"text":"Give it some content by creating a text node","why":"The text is made once the div exists, ready to be attached to it."},{"text":"Add the text node to the newly created div","why":"Attaching the text node makes it a child of the div."},{"text":"Add the newly created element and its content into the DOM","why":"Only the finished div, with its text inside, is inserted before div1."}]},"options":[],"answer":"o:abcd","explanation":"document.createElement() makes a new element that is not in the document yet; you can add content to it, and it appears on the page only once it is inserted.","concept":"document.createElement() makes a new element that is not in the document yet; you can add content to it, and it appears on the page only once it is inserted.","rev":1,"updatedAt":"2026-10-26","kind":"scenario","takeaways":["A new element is invisible until you insert it into the document."]}]