DOM Pitfalls
Null Selectors & Timing
Master the most common DOM errors that trip up JavaScript developers: null references, race conditions, and timing bugs that silently break your code.
What Is the DOM?
The Document Object Model is a tree-shaped representation of your HTML that JavaScript uses to read and modify the page. Every element, attribute, and text node lives in this tree.
How Selectors Find Elements
Methods like querySelector and getElementById search the DOM tree for a matching node. If the element exists, you get a reference. If it doesn't exist, you get null.
The Null Selector Problem
A null selector error happens when your JavaScript tries to use an element that the DOM hasn't created yet, or that simply doesn't exist. This is the single most common DOM bug.
The Core Timing Rule
This is not a bug in JavaScript: it is how the browser's parsing pipeline works. Understanding this single rule prevents the majority of DOM errors.
DOMContentLoaded vs load
The browser fires two key events that signal readiness. Choosing the right one determines when your code can safely touch the DOM.
| DOMContentLoaded | window.onload | |
|---|---|---|
| Fires when | HTML fully parsed, DOM tree built | All resources loaded (images, fonts, CSS) |
| Use for | DOM manipulation | Image dimensions, layout |
| Speed | Fast: fires early | Slow: waits for everything |
| Selectors safe? | ✓ Yes | ✓ Yes |
Three Safe Patterns
defer vs async
Both attributes change how a script loads and executes. They look similar but have very different timing guarantees.
Null-Safety Checklist
Example 1: Script in <head>
A student places their script in the <head> and wonders why nothing works.
Example 2: Dynamic Content
Elements added after page load (via fetch, innerHTML, or frameworks) won't be caught by selectors that already ran.
Example 3: querySelectorAll is Static
Unlike getElementsByClassName, the NodeList from querySelectorAll is a static snapshot. New elements added later will not appear in the list.
Event Listener Timing Traps
Even when your selector works, the listener itself can suffer from timing problems. Two classic traps:
innerHTML Destroys References
Setting innerHTML on a parent replaces all child nodes. Any previously stored references or event listeners on those children become invalid.
Common Mistakes
Summary: Strategies at a Glance
| Strategy | Solves | Best for |
|---|---|---|
| Script at bottom of <body> | Null selectors | Simple pages |
| defer attribute | Null selectors | External scripts |
| DOMContentLoaded | Null selectors | Inline scripts |
| Null check (if / ?.) | Runtime crashes | Defensive coding |
| Event delegation | Dynamic content | SPAs, AJAX |
| AbortController | Double binding | Re-renders |
Question 1 of 5
A <script> tag is placed inside <head> without any attributes. It calls document.querySelector('#app'), where #app is a <div> in <body>. What is the return value?
Question 2 of 5
Which attribute should you add to a <script> tag in <head> to ensure the DOM is fully parsed before the script executes?
Question 3 of 5
You bind a click handler to .card elements at page load. Later, new .card elements are added via an API call. The new cards don't respond to clicks. What is the best fix?
Question 4 of 5
What happens to event listeners on child elements when you set the parent's innerHTML to a new string?
Question 5 of 5
What is the key difference between a static NodeList (from querySelectorAll) and a live HTMLCollection (from getElementsByClassName)?