document.getElementById('x') looks up one element by its id: no selector parsing, only available on document (and DocumentFragment/shadow roots), and returns null if not found. querySelector('selector') accepts any CSS selector, returns the first match in document order, and can be called on any element to search within it. getElementById is marginally faster, but in practice the difference is negligible. Choose based on what you're matching and the scope you need.
<form id="signup">
<input name="email" class="field" />
<button type="submit" class="btn primary">Join</button>
</form>
document.getElementById('signup'); // <form>
document.querySelector('#signup'); // same element
document.querySelector('#signup .btn.primary'); // descendant + multiple classes
form.querySelector('input[name="email"]'); // scoped to the form
document.querySelector('.missing'); // null
getElementById | querySelector | |
|---|---|---|
| Argument | Raw id string, no # | CSS selector |
| Called on | document (and fragments/shadow roots) | document, any element, fragments |
| Returns | Element or null | First match or null |
| Invalid input | Just returns null | Throws SyntaxError for invalid selectors |
| Speed | Direct id map lookup | Parses the selector, then matches (browsers fast-path simple #id) |
| Multiple results | n/a (ids should be unique) | Use querySelectorAll (static NodeList) |
IDs can contain characters that are special in CSS:
// <div id="user:42">, <div id="1st-item">
document.getElementById('user:42'); // works
document.querySelector('#user:42'); // SyntaxError / wrong match (":42" parsed as pseudo-class)
document.querySelector(`#${CSS.escape('1st-item')}`); // works: ids starting with a digit need escaping
Use CSS.escape whenever you build selectors from dynamic values.
const a = document.querySelectorAll('.item'); // static NodeList snapshot
const b = document.getElementsByClassName('item'); // live HTMLCollection
document.body.append(Object.assign(document.createElement('div'), { className: 'item' }));
a.length; // unchanged
b.length; // +1
Static lists are safer to loop over while you mutate the DOM. NodeList from querySelectorAll also has forEach.
getElementById: you have a known, unique id. Clear intent, works with odd characters.querySelector: anything else, especially scoped queries inside a component root (root.querySelector('[data-role="close"]')), attribute selectors, or :scope > li for direct children.element.closest(selector) for going up the tree, typically in event delegation.label for, ARIA references and fragment links.window.signup. Don't rely on it; it's fragile and shadowed by real globals.<head> without defer run before the body is parsed, so you get null.useRef) over global queries; ids may be duplicated when a component renders twice. React's useId helps generate unique ids.document.querySelector doesn't pierce shadow roots; query the shadowRoot itself.CSS.escape for dynamic selectors and the static vs live collection difference.closest, refs in frameworks.Spotted a mistake or a rights concern? Report an issue