The Element.querySelector() method returns the first descendant of this element that matches a CSS selector (MDN). If nothing matches, it returns null.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call el.querySelector. Start with this line.
JavaScript
el.querySelector(selectors)
Call
What it does
el.querySelector(selectors)
Primary signature.
el.querySelector(...)
Call with the arguments your case needs.
Built into the browser. Call querySelector(...) on an Element — no import required.
Library declarations
JavaScript
el.querySelector(selectors)
Parameters
Parameter
Description
Parent element
Required. Search is limited to its descendants
selectors
Required. Valid CSS selector string
Return Value
Return Value
Description
*
First match, or null
JavaScript Examples
Example 1 — First Descendant
Find the first .item inside a list container.
JavaScript
const list = document.getElementById("list");
const out = document.getElementById("out");
const first = list.querySelector(".item");
out.textContent = first ? first.textContent : "null";
Ask for a selector that does not exist under the parent.
MDN: return is null — not an exception.
Compare with === null before using the node.
Invalid CSS is different — that throws SyntaxError.
Compatibility
Browser Support
Element.querySelector() is Baseline Widely available on MDN (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
✓ Baseline Widely available
Element.querySelector()
Find the first matching descendant with CSS selectors. Widely available since 2015.
2015+Widely available
Google ChromeYes
Supported
Mozilla FirefoxYes
Supported
Apple SafariYes
Supported
Microsoft EdgeYes
Supported
OperaYes
Supported
Internet ExplorerIE8+
Partial
querySelector()Baseline
Bottom line: Safe for modern browsers. Prefer querySelector for one match; use querySelectorAll when you need every match. See the MDN querySelector() reference.
Frequently Asked Questions
MDN: it returns the first descendant of the element that matches the given CSS selectors, or null if none match.
No. MDN marks Element.querySelector() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
The first matching descendant Element, or null if no matches are found (MDN).
querySelector() returns the first match (Element or null). querySelectorAll() returns a static NodeList of all matches.
Both use the same selector syntax, but Element.querySelector() only returns matches that are descendants of the element you call it on (MDN).
A SyntaxError DOMException is thrown if selectors is not a valid CSS selector string (MDN).