---
id: api/commands/filter
title: cy.filter()
description: Get the DOM elements that match a specific selector in Cypress.
section: api
source_path: docs/api/commands/filter.mdx
version: 1934fa485ad0afcbbd3bdb674d5c5f1f991b55cf
updated_at: '2026-11-14T05:15:51.878Z'
---
# filter

Get the DOM elements that match a specific selector or pass a test function.

Opposite of [`.not()`](/llm/markdown/api/commands/not.md)

The querying behavior of this command matches exactly how [`.filter()`](https://api.jquery.com/filter/) works in jQuery.

## Syntax

```
.filter(selector)
.filter(selector, options)
.filter(fn)
.filter(fn, options)
```

### Usage

**Correct Usage**

```
cy.get('td').filter('.users') // Yield all el's with class '.users'
```

**Incorrect Usage**

```
cy.filter('.animated') // Errors, cannot be chained off 'cy'
cy.clock().filter() // Errors, 'clock' does not yield DOM elements
```

### Arguments

**selector _(String selector)_**

A selector used to filter matching DOM elements.

**fn _(Function)_**

A function called once for each element in the subject, with the element's index and the element itself as arguments: `(index, element)`. `.filter()` yields the elements for which the function returns a truthy value.

**options _(Object)_**

Pass in an options object to change the default behavior of `.filter()`.

| Option | Default | Description |
| --- | --- | --- |
| `log` | `true` | Displays the command in the [Command log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |
| `timeout` | [`defaultCommandTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `.filter()` to resolve before [timing out](#Timeouts) |

### Yields

*   `.filter()` yields the new DOM element(s) it found.
*   `.filter()` is a query, and it is _safe_ to chain further commands.

## Examples

### Selector

#### Filter list items by class name with `.filter()`

index.html

```
<ul>
  <li>Home</li>
  <li class="active">About</li>
  <li>Services</li>
  <li>Pricing</li>
  <li>Contact</li>
</ul>
```

spec.cy.js

```
// yields <li>About</li>
cy.get('ul').find('>li').filter('.active')
```

#### Filter elements by attribute value with `.filter()`

index.html

```
<table>
  <tr data-cy="order-row" data-status="shipped">
    <td>#1001</td>
  </tr>
  <tr data-cy="order-row" data-status="pending">
    <td>#1002</td>
  </tr>
  <tr data-cy="order-row" data-status="shipped">
    <td>#1003</td>
  </tr>
</table>
```

spec.cy.js

```
// yields the rows for orders #1001 and #1003
cy.get('[data-cy="order-row"]')
  .filter('[data-status="shipped"]')
  .should('have.length', 2)
```

#### Filter form controls by checked or disabled state

Standard CSS pseudo-classes such as `:checked`, `:disabled`, and `:enabled` narrow form controls by their current state.

index.html

```
<input type="checkbox" name="email" checked />
<input type="checkbox" name="sms" />
<input type="checkbox" name="push" checked />
```

spec.cy.js

```
cy.get('input[type="checkbox"]').filter(':checked').should('have.length', 2)
```

#### Filter to visible elements with the `:visible` selector

Cypress replaces jQuery's `:visible` and `:hidden` selectors with its own [visibility rules](/llm/markdown/app/core-concepts/interacting-with-elements.md#Visibility), so `.filter(':visible')` keeps exactly the elements that `.should('be.visible')` treats as visible. Cypress also adds a `:focused` selector that matches the focused element.

index.html

```
<ul class="menu">
  <li>Dashboard</li>
  <li>Reports</li>
  <li style="display: none">Admin</li>
  <li>Settings</li>
</ul>
```

spec.cy.js

```
cy.get('.menu li').filter(':visible').should('have.length', 3)
```

### Function

#### Filter elements with a test function in `.filter()`

index.html

```
<ul>
  <li>Home</li>
  <li class="active">About</li>
  <li>Services</li>
</ul>
```

spec.cy.js

```
// yields <li>Home</li> and <li>Services</li>
cy.get('li').filter((index, el) => !el.classList.contains('active'))
```

spec.cy.js

```
// yields the even-indexed <li> elements: Home and Services
cy.get('li').filter((index) => index % 2 === 0)
```

Because `.filter()` is a query, Cypress calls the function again on every [retry](/llm/markdown/app/core-concepts/retry-ability.md). Keep it synchronous and free of side effects, and don't call other Cypress commands inside it.

Read the element from the function's second argument rather than from `this`. Arrow functions don't bind `this`, so `el` works in every function style.

#### Filter elements by a numeric value with a function

A selector can't compare numbers, but a function can.

index.html

```
<ul>
  <li class="price">$45.00</li>
  <li class="price">$120.00</li>
  <li class="price">$310.50</li>
</ul>
```

spec.cy.js

```
// yields the $120.00 and $310.50 prices
cy.get('.price')
  .filter((index, el) => parseFloat(el.textContent.replace('$', '')) > 100)
  .should('have.length', 2)
```

#### Filter elements by text without matching case

The `:contains` selector is case-sensitive. Compare lowercased text in a function to match regardless of case.

index.html

```
<ul>
  <li>Services</li>
  <li>Advanced services</li>
  <li>Pricing</li>
</ul>
```

spec.cy.js

```
cy.get('li')
  .filter((index, el) => el.textContent.toLowerCase().includes('services'))
  .should('have.length', 2)
```

To get a single element by text regardless of case, use [`.contains()`](/llm/markdown/api/commands/contains.md) with the `matchCase: false` option.

### Contains

#### Filter elements by text with the `:contains` selector

You can use the [jQuery :contains](https://api.jquery.com/contains-selector/) selector to perform a case-sensitive text substring match.

index.html

```
<ul>
  <li>Home</li>
  <li>Services</li>
  <li>Advanced Services</li>
  <li>Pricing</li>
  <li>Contact</li>
</ul>
```

Find both list items that contain the word "Services":

spec.cy.js

```
cy.get('li').filter(':contains("Services")').should('have.length', 2)
```

#### Filter table rows by a cell's text with `:has()`

Combine the [jQuery :has](https://api.jquery.com/has-selector/) selector with `:contains` to keep only the rows with a matching cell, then act on something inside that row.

index.html

```
<table>
  <tbody>
    <tr>
      <td>Ana</td>
      <td>Admin</td>
      <td><button class="edit">Edit</button></td>
    </tr>
    <tr>
      <td>Ben</td>
      <td>Viewer</td>
      <td><button class="edit">Edit</button></td>
    </tr>
  </tbody>
</table>
```

spec.cy.js

```
cy.get('tbody tr')
  .filter(':has(td:contains("Admin"))')
  .find('button.edit')
  .click()
```

#### Match a non-breaking space with `:contains` in `.filter()`

If the HTML contains a [non-breaking space](https://en.wikipedia.org/wiki/Non-breaking_space) entity `&nbsp;` and the test uses the [jQuery :contains](https://api.jquery.com/contains-selector/) selector, then the test needs to use the Unicode value `\u00a0` instead of `&nbsp;`.

index.html

```
<div data-testid="testattr">
  <span>Hello&nbsp;world</span>
</div>
```

spec.cy.js

```
cy.get('[data-testid="testattr"]').filter(':contains("Hello\u00a0world")')
```

### Actions and assertions

#### Click one element from a `.filter()` result

`.filter()` can yield several elements, and action commands such as [`.click()`](/llm/markdown/api/commands/click.md) fail on more than one element unless you pass `{ multiple: true }`. Narrow the result to a single element first with [`.first()`](/llm/markdown/api/commands/first.md), [`.last()`](/llm/markdown/api/commands/last.md), or [`.eq()`](/llm/markdown/api/commands/eq.md).

index.html

```
<ul>
  <li><button class="delete">Delete</button> Draft 1</li>
  <li><button class="delete" disabled>Delete</button> Published post</li>
  <li><button class="delete">Delete</button> Draft 2</li>
</ul>
```

spec.cy.js

```
// clicks the Delete button for Draft 1
cy.get('button.delete').filter(':enabled').first().click()
```

#### Assert that no elements match a `.filter()` selector

index.html

```
<ul>
  <li class="notification info">Saved</li>
  <li class="notification info">Synced</li>
</ul>
```

spec.cy.js

```
// passes once no notification has the 'error' class
cy.get('.notification').filter('.error').should('not.exist')
```

## Notes

### Choosing between `.filter()`, `.find()`, and `.contains()`

These three commands narrow a selection in different ways:

*   `.filter()` keeps the elements in the current subject that match. It never looks inside them.
*   [`.find()`](/llm/markdown/api/commands/find.md) searches the descendants of the current subject.
*   [`.contains()`](/llm/markdown/api/commands/contains.md) matches by text and yields the deepest element that contains it.

index.html

```
<ul>
  <li class="active">About <span class="badge">New</span></li>
  <li>Services</li>
</ul>
```

spec.cy.js

```
cy.get('li').filter('.active') // yields <li class="active">
cy.get('li').find('.badge') // yields <span class="badge">
cy.get('ul').contains('Services') // yields <li>Services</li>
```

## Rules

### Requirements

*   `.filter()` requires being chained off a command that yields DOM element(s) or a document.

### Assertions

*   `.filter()` will automatically [retry](/llm/markdown/app/core-concepts/retry-ability.md) until the element(s) [exist in the DOM](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Implicit-Assertions).
*   `.filter()` will automatically [retry](/llm/markdown/app/core-concepts/retry-ability.md) until all chained assertions have passed.

### Timeouts

*   `.filter()` can time out waiting for the element(s) to [exist in the DOM](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Implicit-Assertions).
*   `.filter()` can time out waiting for assertions you've added to pass.

## Command Log

**_Filter the li's to the li with the class 'active'._**

```
cy.get('.left-nav>.nav').find('>li').filter('.active')
```

The commands above display in the Command Log as:

Clicking the `filter` command in the Command Log outputs the following to the console:

## History

| Version | Changes |
| --- | --- |
| [12.6.0](/llm/markdown/app/references/changelog.md#12-6-0) | Overwrite `.filter()` with [`Cypress.Commands.overwriteQuery()`](/llm/markdown/api/cypress-api/custom-queries.md#Overwriting-Existing-Queries) rather than `Cypress.Commands.overwrite()`. |
| [12.0.0](/llm/markdown/app/references/changelog.md#12-0-0) | `.filter()` is now a query. |
| [3.2.0](/llm/markdown/app/references/changelog.md#3-2-0) | `.filter()` reads `options` when its first argument is a function. |
| [3.0.0](/llm/markdown/app/references/changelog.md#3-0-0) | The type definition accepts a function argument. |

## See also

*   [`.not()`](/llm/markdown/api/commands/not.md)
*   [`.find()`](/llm/markdown/api/commands/find.md)
*   [`.contains()`](/llm/markdown/api/commands/contains.md)
*   [`.first()`](/llm/markdown/api/commands/first.md)
