DOM Element.querySelector() Method
The querySelector() method returns the first element inside an element that matches a CSS selector, or null if nothing matches.
Description
The querySelector() method of the Element interface returns the first descendant of the element that matches the given CSS selectors, searching in document order. If nothing matches, it returns null, so check the result before using it.
Any valid CSS selector works, including classes, IDs, attribute selectors, pseudo-classes such as :checked and combinators. The element itself is not included in the search, but the selector is matched against the whole document, so a selector like div p can match a paragraph whose div ancestor is outside the element.
To get every match rather than the first, use querySelectorAll(), which returns a static NodeList. document.querySelector() works in the same way across the whole document.
Syntax
element.querySelector(selectors)Parameters
- selectorsA string containing one or more CSS selectors, separated by commas.
Return Value
The first descendant element that matches the selectors, or null if there is no match.
Exceptions
- SyntaxErrorThrown if the selectors are not valid CSS.
Example
Browser Support
The following information will show you the current browser support for the Element.querySelector() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





