--- title: Element.querySelector() slug: Web/API/Element/querySelector translation_of: Web/API/Element/querySelector ---
Returns the first element that is a descendant of the element on which it is invoked that matches the specified group of selectors.
element = baseElement.querySelector(selectors);
selectors
baseElement
against; this must be valid CSS syntax, or a SyntaxError
exception will occur. The first element found which matches this group of selectors is returned.The first descendant element of baseElement
which matches the specified group of selectors
. The entire hierarchy of elements is considered when matching, including those outside the set of elements including baseElement
and its descendants; in other words, selectors
is first applied to the whole document, not the baseElement
, to generate an initial list of potential elements. The resulting elements are then examined to see if they are descendants of baseElement
. The first match of those remaining elements is returned by the querySelector()
method.
If no matches are found, the returned value is null
.
SyntaxError
selectors
are invalid.Let's consider a few examples.
In this first example, the first {{HTMLElement("style")}} element which either has no type or has type "text/css" in the HTML document body is returned:
var el = document.body.querySelector("style[type='text/css'], style:not([type])");
The next example, below, demonstrates that the hierarchy of the entire document is considered when applying selectors
, so that levels which are outside the specified baseElement
are still considered when locating matches.
<div> <h5>Original content</h5> <p> inside paragraph <span>inside span</span> inside paragraph </p> </div> <div> <h5>Output</h5> <div id="output"></div> </div>
var baseElement = document.querySelector("p"); document.getElementById("output").innerHTML = (baseElement.querySelector("div span").innerHTML);
The result looks like this:
{{ EmbedLiveSample('The_entire_hierarchy_counts', 600, 160) }}
Notice how the "div span"
selector still matches the {{HTMLElement("span")}} element, even though the baseElement
excludes the {{domxref("div")}} element which is part of the specified selector.
The :scope pseudo-class restores the expected behavior, only matching selectors on descendants of the baseElement.
See {{domxref("Document.querySelector()")}} for additional examples of the proper format for the selectors
.
Specification | Status | Comment |
---|---|---|
{{SpecName('DOM4','#dom-parentnode-queryselectorallselectors','querySelectorAll()')}} | {{Spec2('DOM4')}} | |
{{SpecName('Selectors API Level 2','#queryselectorall','querySelectorAll()')}} | {{Spec2('Selectors API Level 2')}} | |
{{SpecName('Selectors API Level 1','#queryselectorall','querySelectorAll()')}} | {{Spec2('Selectors API Level 1')}} |
{{ CompatibilityTable}}
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | {{CompatChrome(1)}} | 12 | {{CompatGeckoDesktop(1.9.1)}} | 9[1][2] | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
[1] querySelector()
is supported in IE8, but only for CSS 2.1 selectors.
[2] in IE8 and iE9 document must be in HTML5 mode (HTML5 doctype declaration present)