querySelector()

Introduction to JavaScript querySelector() and querySelectorAll() methods

The querySelector() is a method of the Element interface. The querySelector() method allows you to select the first element that matches one or more CSS selectors.

The following illustrates the syntax of the querySelector() method:

let element = parentNode.querySelector(selector);Code language: JavaScript (javascript)

In this syntax, the selector is a CSS selector or a group of CSS selectors to match the descendant elements of the parentNode.

If the selector is not valid CSS syntax, the method will raise a SyntaxError exception.

If no element matches the CSS selectors, the querySelector() returns null.

The querySelector() method is available on the document object or any Element object.

Besides the querySelector(), you can use the querySelectorAll() method to select all elements that match a CSS selector or a group of CSS selectors:

let elementList = parentNode.querySelectorAll(selector);Code language: JavaScript (javascript)

The querySelectorAll() method returns a static NodeList of elements that match the CSS selector. If no element matches, it returns an empty NodeList.

Note that the NodeList is an array-like object, not an array object. However, in modern web browsers, you can use the forEach() method or the for...of loop.

To convert the NodeList to an array, you use the Array.from() method like this:

let nodeList = document.querySelectorAll(selector);
let elements = Array.from(nodeList);Code language: JavaScript (javascript)

Basic selectors

Suppose that you have the following HTML document:

1) Universal selector

The universal selector is denoted by * that matches all elements of any type:

The following example uses the querySelector() selects the first element in the document:

And this select all elements in the document:

2) Type selector

To select elements by node name, you use the type selector e.g., a selects all <a> elements:

The following example finds the first h1 element in the document:

And the following example finds all h2 elements:

3) Class selector

To find the element with a given CSS class, you use the class selector syntax:

The following example finds the first element with the menu-item class:

And the following example finds all elements with the menu class:

4) ID Selector

To select an element based on the value of its id, you use the id selector syntax:

The following example finds the first element with the id #logo:

Since the id should be unique in the document, the querySelectorAll() is not relevant.

5) Attribute selector

To select all elements that have a given attribute, you use one of the following attribute selector syntaxes:

The following example finds the first element with the attribute [autoplay] with any value:

And the following example finds all elements that have [autoplay] attribute with any value:

Grouping selectors

To group multiple selectors, you use the following syntax:

The selector list will match any element with one of the selectors in the group.

The following example finds all <div> and <p> elements:

Combinators

1) descendant combinator

To find descendants of a node, you use the space ( ) descendant combinator syntax:

For example p a will match all <a> elements inside the p element:

2) Child combinator

The > child combinator finds all elements that are direct children of the first element:

The following example finds all li elements that are directly inside a <ul> element:

To select all li elements that are directly inside a <ul> element with the class nav:

3) General sibling combinator

The ~ combinator selects siblings that share the same parent:

For example, p ~ a will match all <a> elements that follow the p element, immediately or not:

4) Adjacent sibling combinator

The + adjacent sibling combinator selects adjacent siblings:

For example, h1 + a matches all elements that directly follow an h1:

And select the first <a> that directly follows an h1:

Pseudo

1) Pseudo-classes

The : pseudo matches elements based on their states:

For example, the li:nth-child(2) selects the second <li> element in a list:

2) Pseudo-elements

The :: represent entities that are not included in the document.

For example, p::first-line matches the first line of all p elements:

Summary

  • The querySelector() finds the first element that matches a CSS selector or a group of CSS selectors.

  • The querySelectorAll() finds all elements that match a CSS selector or a group of CSS selectors.

  • A CSS selector defines elements to which a CSS rule applies.

Last updated