HTML DOM Document querySelector()
Examples
Get the first <p> element:
document.querySelector("p");
Try it Yourself »
Get the first element with class="example":
document.querySelector(".example");
Try it Yourself »
More examples below.
Definition and Usage
The querySelector()
method returns the first element that matches a CSS selector.
To return all matches (not only the first), use the querySelectorAll()
instead.
Both querySelector()
and querySelectorAll()
throw a SYNTAX_ERR exception if the selector(s) is invalid.
Tutorials:
The HTML DOM NodeList Reference
QuerySelector Methods:
GetElement Methods:
The Difference Between an HTMLCollection and a NodeList
A NodeList and an HTMLcollection is very much the same thing.
Both are array-like collections (lists) of nodes (elements) extracted from a document. The nodes can be accessed by index numbers. The index starts at 0.
Both have a length property that returns the number of elements in the list (collection).
An HTMLCollection is a collection of document elements.
A NodeList is a collection of document nodes (element nodes, attribute nodes, and text nodes).
HTMLCollection items can be accessed by their name, id, or index number.
NodeList items can only be accessed by their index number.
An HTMLCollection is always a live collection. Example: If you add a <li> element to a list in the DOM, the list in the HTMLCollection will also change.
A NodeList is most often a static collection. Example: If you add a <li> element to a list in the DOM, the list in NodeList will not change.
The getElementsByClassName()
and getElementsByTagName()
methods return a live HTMLCollection.
The querySelectorAll()
method returns a static NodeList.
The childNodes
property returns a live NodeList.
Syntax
document.querySelector(CSS selectors)
Parameters
Parameter | Description |
CSS selectors |
Required. One or more CSS selectors. CSS selectors select HTML elements based on id, classes, types, attributes, values of attributes etc. For a full list, go to our CSS Selectors Reference. For multiple selectors, separate each selector with a comma (See "More Examples"). |
Return Value
Type | Description |
Object | A NodeList with the first element that matches the CSS selector(s). If no matches are found, null is returned. |
More Examples
Get the first <p> element in with class="example":
document.querySelector("p.example");
Try it Yourself »
Change the text of the element with id="demo":
document.querySelector("#demo").innerHTML = "Hello World!";
Try it Yourself »
Select the first <p> element with the parent is a <div> element.
document.querySelector("div > p");
Try it Yourself »
Select the first <a> element that has a "target" attribute:
document.querySelector("a[target]");
Try it Yourself »
Select the first <h3> or the first <h4>:
<h3>A h3 element</h3>
<h4>A h4 element</h4>
document.querySelector("h3, h4").style.backgroundColor = "red";
Try it Yourself »
Select the first <h3> or the first <h4>:
<h4>A h4 element</h4>
<h3>A h3 element</h3>
document.querySelector("h3, h4").style.backgroundColor = "red";
Try it Yourself »
Browser Support
document.querySelector()
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |