The DOM (Document Object Model) is the browser's in-memory tree of an HTML page. For interviews, you need to know how to find elements, read their properties, traverse the tree, and understand the difference between inline styles and computed styles.
1. Finding Elements
Loading editor...
querySelector vs getElementBy*: The querySelector methods return static NodeLists. The getElementBy* methods return live HTMLCollections — they update automatically when the DOM changes.
2. Element Properties
Loading editor...
3. classList API
Loading editor...
Interview use: getElementsByClassName (#24) checks element.classList.contains(className) to match descendants.
4. Attributes
Loading editor...
5. Traversing the Tree
Loading editor...
Important: children returns only element nodes. childNodes returns ALL nodes (text nodes, comments, etc.). Always use children for tree traversal — it's what the native DOM methods traverse.
Interview use: DFS walk in getElementsByClassName (#24) — iterate element.children.
6. Creating, Inserting, and Removing Elements
Loading editor...
7. Styles
Inline Styles (element.style)
Loading editor...
Computed Styles (getComputedStyle)
Loading editor...
Key difference: element.style only reads inline styles. getComputedStyle(element) reads the final applied style after all CSS rules and inheritance are resolved.
Interview use: getElementsByStyle (#24) — use getComputedStyle to check the actual rendered style.
8. Events
Loading editor...
9. Data Storage on Elements
Loading editor...
Quick Reference
| Task | Method/Property |
|---|---|
| Find first | querySelector(css) |
| Find all | querySelectorAll(css) |
| Tag name | element.tagName (UPPERCASE in HTML) |
| Class check | element.classList.contains(cls) |
| Child elements | element.children (elements only) |
| Parent | element.parentElement |
| Inline style | element.style.property |
| Computed style | getComputedStyle(element).property |
| Create | document.createElement(tag) |
| Append | parent.appendChild(el) or parent.append(el) |
| Remove | el.remove() |
Interview Tips
- Use
element.children, notchildNodes—childNodesincludes text nodes and comments. - Tag names are UPPERCASE in HTML —
element.tagNamereturns'DIV', not'div'. Use.toUpperCase()for case-insensitive comparison. getComputedStyle, notelement.style— the interviewer wants to see you know the difference.- DFS over
childrenpreserves document order — the native DOM methods return elements in document order, which is DFS pre-order.