---
id: api/commands/filter
title: filter | Cypress Documentation
description: Get the DOM elements that match a specific selector in Cypress.
section: api
source_path: docs/api/commands/filter.mdx
version: ce02913654e2655ee63448bdc92bb92c7b46a619
updated_at: '2026-04-22T19:37:51.587Z'
---
# filter

Get the DOM elements that match a specific selector.

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

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

## Syntax

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

### Usage

 **Correct Usage**

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

 **Incorrect Usage**

```javascript
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.

 **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 [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)

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

## Examples

### Selector

#### Filter the current subject to the elements with the class 'active'

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

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

### Contains

#### Filter by text

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

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

Let's find both list items that contain the work "Services"

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

#### Non-breaking space

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;`.

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

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

## Rules

### Requirements [Learn about chaining commands](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands)

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

### Assertions [Learn about assertions](/llm/markdown/app/core-concepts/introduction-to-cypress.md#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 [Learn about timeouts](/llm/markdown/app/core-concepts/introduction-to-cypress.md#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'.***

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

The commands above will display in the Command Log as:

When clicking on the `filter` command within the command log, the console
outputs the following:

## See also

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