JavaScript Element matches() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

The Element.matches() method tests whether this element would be selected by a CSS selector (MDN). It returns true or false.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call el.matches. Start with this line.

JavaScript
el.matches(selectors)
CallWhat it does
el.matches(selectors)Primary signature.
el.matches(...)Call with the arguments your case needs.

Built into the browser. Call matches(...) on an Element — no import required.

Library declarations
JavaScript
el.matches(selectors)

Parameters

ParameterDescription
selectorsRequired. Valid CSS selector string

Return Value

Return ValueDescription
*true or false

JavaScript Examples

Example 1 — Filter With matches() (MDN)

Loop list items and log only elements that match .endangered.

JavaScript
const birds = document.querySelectorAll("li");
const out = document.getElementById("out");
const lines = [];

for (const bird of birds) {
  if (bird.matches(".endangered")) {
    lines.push("The " + bird.textContent + " is endangered!");
  }
}
out.textContent = lines.join("\\n") || "(none)";
Try It Yourself

How It Works

  1. Collect candidates. querySelectorAll("li") returns every list item.
  2. Test each one. matches(".endangered") is a boolean check.
  3. Keep matches only. The eagle has that class; others do not.
  4. Show the result. Same idea as MDN’s console log.

Example 2 — True / False Check

Ask whether one element matches a selector.

JavaScript
const btn = document.getElementById("save");
const out = document.getElementById("out");

out.textContent = String(btn.matches("button[type='submit']"));
Try It Yourself

How It Works

  1. Grab one element. Here, the save button.
  2. Pass attribute CSS. button[type='submit'] is a normal selector.
  3. Get a boolean. true means this node would be selected.
  4. Branch freely. Use it in if, filters, or guards.

Example 3 — Click Filter With matches()

Ignore clicks unless the target itself is a .item button.

JavaScript
const list = document.getElementById("list");
const out = document.getElementById("out");

list.addEventListener("click", (event) => {
  const el = event.target;
  if (!(el instanceof Element) || !el.matches("button.item")) return;
  out.textContent = "clicked " + el.textContent;
});
Try It Yourself

How It Works

  1. Listen on a parent. One handler covers many children.
  2. Test the target. matches("button.item") requires the exact node type.
  3. Return early when the click is on something else.
  4. Prefer closest() if clicks may land on nested text/spans inside the button.

Browser Support

Element.matches() is Baseline Widely available on MDN (across browsers since April 2017). It is not Deprecated, Experimental, or Non-standard.

✓ Baseline Widely available

Element.matches()

Test whether an element matches a CSS selector. Widely available since 2017.

2017+ Widely available
Google Chrome Yes
Supported
Mozilla Firefox Yes
Supported
Apple Safari Yes
Supported
Microsoft Edge Yes
Supported
Opera Yes
Supported
Internet Explorer IE9+ (msMatchesSelector)
Partial
matches() Baseline

Bottom line: Safe for modern browsers. Prefer matches() over vendor-prefixed helpers. See the MDN matches() reference.

Frequently Asked Questions

MDN: it tests whether the element would be selected by the given CSS selector string. It returns true if the element matches, otherwise false.
No. MDN marks Element.matches() as Baseline Widely available (across browsers since April 2017). It is not Deprecated, Experimental, or Non-standard.
A boolean: true if the element matches the selectors, false if it does not (MDN).
matches() tests only the current element. closest() walks up through ancestors (including self) and returns the first matching Element or null.
Yes. matches() accepts the same valid CSS selector strings as querySelector and querySelectorAll (MDN).
A SyntaxError DOMException is thrown if selectors cannot be parsed as a valid CSS selector list (MDN).

Next: moveBefore()

Continue with the Element moveBefore() method.

moveBefore() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful