matchesSelector

Commons
stable

Safely tests whether a DOM element matches a CSS selector. Returns null when the browser does not support the CSS.supports selector() query, and null on error.

API

Signature

Full TypeScript signature sourced from the util file.

typescript

API

Parameters

Input parameters accepted by this util.

el
required
Element | undefined
The DOM element to test. Returns null when undefined.
selector
required
string
A valid CSS selector string (e.g. ":focus-visible", "[data-active]").

API

Return value

The value returned by this util.

true / false when the browser supports selector() and el.matches() succeeds. null when unsupported, el is undefined, or an error is thrown.

Usage

Examples

Ready-to-paste usage snippets.

Check if an element is focused

typescript