Favorites

DOM Element.querySelector() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Element.querySelector()
DOM Element MethodThis method is supported by all modern browsers.
Updated 28th November 2026

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

<ul id="menu">
  <li>Home</li>
  <li class="active">Blog</li>
  <li>Contact</li>
</ul>
const menu = document.querySelector('#menu');

const active = menu.querySelector('.active');
console.log(active.textContent);

console.log(menu.querySelector('li:last-child').textContent);
console.log(menu.querySelector('.missing'));   // no match

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee