{
  "doc": {
    "id": "api/commands/filter",
    "title": "filter | Cypress Documentation",
    "description": "Get the DOM elements that match a specific selector in Cypress.",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/filter.md",
    "version": "a8fd16711bdda4c7b5645b9717e588ae99ec2470",
    "updated_at": "2026-05-18T17:21:32.047Z",
    "headings": [
      {
        "id": "api/commands/filter#filter",
        "text": "filter",
        "level": 1
      },
      {
        "id": "api/commands/filter#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/filter#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/filter#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/filter#yields-learn-about-subject-management",
        "text": "Yields Learn about subject management",
        "level": 3
      },
      {
        "id": "api/commands/filter#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/filter#selector",
        "text": "Selector",
        "level": 3
      },
      {
        "id": "api/commands/filter#filter-the-current-subject-to-the-elements-with-the-class-active",
        "text": "Filter the current subject to the elements with the class 'active'",
        "level": 4
      },
      {
        "id": "api/commands/filter#contains",
        "text": "Contains",
        "level": 3
      },
      {
        "id": "api/commands/filter#filter-by-text",
        "text": "Filter by text",
        "level": 4
      },
      {
        "id": "api/commands/filter#non-breaking-space",
        "text": "Non-breaking space",
        "level": 4
      },
      {
        "id": "api/commands/filter#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/filter#requirements-learn-about-chaining-commands",
        "text": "Requirements Learn about chaining commands",
        "level": 3
      },
      {
        "id": "api/commands/filter#assertions-learn-about-assertions",
        "text": "Assertions Learn about assertions",
        "level": 3
      },
      {
        "id": "api/commands/filter#timeouts-learn-about-timeouts",
        "text": "Timeouts Learn about timeouts",
        "level": 3
      },
      {
        "id": "api/commands/filter#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/filter#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "chunks": [
    {
      "id": "api/commands/filter#syntax",
      "doc_id": "api/commands/filter",
      "heading": "Syntax",
      "heading_level": 2,
      "content_markdown": "## Syntax\n\n```\n.filter(selector).filter(selector, options)\n```\n\n### Usage\n\n**Correct Usage**\n\n```\ncy.get('td').filter('.users') // Yield all el's with class '.users'\n```\n\n**Incorrect Usage**\n\n```\ncy.filter('.animated') // Errors, cannot be chained off 'cy'cy.clock().filter() // Errors, 'clock' does not yield DOM elements\n```\n\n### Arguments\n\n**selector _(String selector)_**\n\nA selector used to filter matching DOM elements.\n\n**options _(Object)_**\n\nPass in an options object to change the default behavior of `.filter()`.\n\n| Option | Default | Description |\n| --- | --- | --- |\n| `log` | `true` | Displays the command in the [Command log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |\n| `timeout` | [`defaultCommandTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `.filter()` to resolve before [timing out](#Timeouts) |\n\n### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)\n\n*   `.filter()` yields the new DOM element(s) it found.\n*   `.filter()` is a query, and it is _safe_ to chain further commands.\n",
      "section": "api",
      "anchors": [
        "syntax"
      ],
      "path": "/llm/json/chunked/api/commands/filter.json",
      "token_estimate": 184
    },
    {
      "id": "api/commands/filter#usage",
      "doc_id": "api/commands/filter",
      "heading": "Usage",
      "heading_level": 3,
      "content_markdown": "### Usage\n\n**Correct Usage**\n\n```\ncy.get('td').filter('.users') // Yield all el's with class '.users'\n```\n\n**Incorrect Usage**\n\n```\ncy.filter('.animated') // Errors, cannot be chained off 'cy'cy.clock().filter() // Errors, 'clock' does not yield DOM elements\n```\n",
      "section": "api",
      "anchors": [
        "usage"
      ],
      "path": "/llm/json/chunked/api/commands/filter.json",
      "token_estimate": 45
    },
    {
      "id": "api/commands/filter#arguments",
      "doc_id": "api/commands/filter",
      "heading": "Arguments",
      "heading_level": 3,
      "content_markdown": "### Arguments\n\n**selector _(String selector)_**\n\nA selector used to filter matching DOM elements.\n\n**options _(Object)_**\n\nPass in an options object to change the default behavior of `.filter()`.\n\n| Option | Default | Description |\n| --- | --- | --- |\n| `log` | `true` | Displays the command in the [Command log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |\n| `timeout` | [`defaultCommandTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `.filter()` to resolve before [timing out](#Timeouts) |\n",
      "section": "api",
      "anchors": [
        "arguments"
      ],
      "path": "/llm/json/chunked/api/commands/filter.json",
      "token_estimate": 93
    },
    {
      "id": "api/commands/filter#examples",
      "doc_id": "api/commands/filter",
      "heading": "Examples",
      "heading_level": 2,
      "content_markdown": "## Examples\n\n### Selector\n\n#### Filter the current subject to the elements with the class 'active'\n\n```\n<ul>  <li>Home</li>  <li class=\"active\">About</li>  <li>Services</li>  <li>Pricing</li>  <li>Contact</li></ul>\n```\n\n```\n// yields <li>About</li>cy.get('ul').find('>li').filter('.active')\n```\n\n### Contains\n\n#### Filter by text\n\nYou can use the [jQuery :contains](https://api.jquery.com/contains-selector/) selector to perform a case-sensitive text substring match.\n\n```\n<ul>  <li>Home</li>  <li>Services</li>  <li>Advanced Services</li>  <li>Pricing</li>  <li>Contact</li></ul>\n```\n\nLet's find both list items that contain the work \"Services\"\n\n```\ncy.get('li').filter(':contains(\"Services\")').should('have.length', 2)\n```\n\n#### Non-breaking space\n\nIf 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;`.\n\n```\n<div data-testid=\"testattr\">  <span>Hello&nbsp;world</span></div>\n```\n\n```\ncy.get('[data-testid=\"testattr\"]').filter(':contains(\"Hello\\u00a0world\")')\n```\n",
      "section": "api",
      "anchors": [
        "examples"
      ],
      "path": "/llm/json/chunked/api/commands/filter.json",
      "token_estimate": 152
    },
    {
      "id": "api/commands/filter#contains",
      "doc_id": "api/commands/filter",
      "heading": "Contains",
      "heading_level": 3,
      "content_markdown": "### Contains\n\n#### Filter by text\n\nYou can use the [jQuery :contains](https://api.jquery.com/contains-selector/) selector to perform a case-sensitive text substring match.\n\n```\n<ul>  <li>Home</li>  <li>Services</li>  <li>Advanced Services</li>  <li>Pricing</li>  <li>Contact</li></ul>\n```\n\nLet's find both list items that contain the work \"Services\"\n\n```\ncy.get('li').filter(':contains(\"Services\")').should('have.length', 2)\n```\n\n#### Non-breaking space\n\nIf 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;`.\n\n```\n<div data-testid=\"testattr\">  <span>Hello&nbsp;world</span></div>\n```\n\n```\ncy.get('[data-testid=\"testattr\"]').filter(':contains(\"Hello\\u00a0world\")')\n```\n",
      "section": "api",
      "anchors": [
        "contains"
      ],
      "path": "/llm/json/chunked/api/commands/filter.json",
      "token_estimate": 112
    },
    {
      "id": "api/commands/filter#filter-by-text",
      "doc_id": "api/commands/filter",
      "heading": "Filter by text",
      "heading_level": 4,
      "content_markdown": "#### Filter by text\n\nYou can use the [jQuery :contains](https://api.jquery.com/contains-selector/) selector to perform a case-sensitive text substring match.\n\n```\n<ul>  <li>Home</li>  <li>Services</li>  <li>Advanced Services</li>  <li>Pricing</li>  <li>Contact</li></ul>\n```\n\nLet's find both list items that contain the work \"Services\"\n\n```\ncy.get('li').filter(':contains(\"Services\")').should('have.length', 2)\n```\n",
      "section": "api",
      "anchors": [
        "filter-by-text"
      ],
      "path": "/llm/json/chunked/api/commands/filter.json",
      "token_estimate": 55
    },
    {
      "id": "api/commands/filter#non-breaking-space",
      "doc_id": "api/commands/filter",
      "heading": "Non-breaking space",
      "heading_level": 4,
      "content_markdown": "#### Non-breaking space\n\nIf 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;`.\n\n```\n<div data-testid=\"testattr\">  <span>Hello&nbsp;world</span></div>\n```\n\n```\ncy.get('[data-testid=\"testattr\"]').filter(':contains(\"Hello\\u00a0world\")')\n```\n",
      "section": "api",
      "anchors": [
        "non-breaking-space"
      ],
      "path": "/llm/json/chunked/api/commands/filter.json",
      "token_estimate": 55
    },
    {
      "id": "api/commands/filter#rules",
      "doc_id": "api/commands/filter",
      "heading": "Rules",
      "heading_level": 2,
      "content_markdown": "## Rules\n\n### Requirements [Learn about chaining commands](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands)\n\n*   `.filter()` requires being chained off a command that yields DOM element(s).\n\n### Assertions [Learn about assertions](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions)\n\n*   `.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).\n*   `.filter()` will automatically [retry](/llm/markdown/app/core-concepts/retry-ability.md) until all chained assertions have passed.\n\n### Timeouts [Learn about timeouts](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Timeouts)\n\n*   `.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).\n*   `.filter()` can time out waiting for assertions you've added to pass.\n",
      "section": "api",
      "anchors": [
        "rules"
      ],
      "path": "/llm/json/chunked/api/commands/filter.json",
      "token_estimate": 105
    },
    {
      "id": "api/commands/filter#timeouts-learn-about-timeouts",
      "doc_id": "api/commands/filter",
      "heading": "Timeouts Learn about timeouts",
      "heading_level": 3,
      "content_markdown": "### Timeouts [Learn about timeouts](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Timeouts)\n\n*   `.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).\n*   `.filter()` can time out waiting for assertions you've added to pass.\n",
      "section": "api",
      "anchors": [
        "timeouts-learn-about-timeouts"
      ],
      "path": "/llm/json/chunked/api/commands/filter.json",
      "token_estimate": 41
    },
    {
      "id": "api/commands/filter#command-log",
      "doc_id": "api/commands/filter",
      "heading": "Command Log",
      "heading_level": 2,
      "content_markdown": "## Command Log\n\n**_Filter the li's to the li with the class 'active'._**\n\n```\ncy.get('.left-nav>.nav').find('>li').filter('.active')\n```\n\nThe commands above will display in the Command Log as:\n\nWhen clicking on the `filter` command within the command log, the console outputs the following:\n",
      "section": "api",
      "anchors": [
        "command-log"
      ],
      "path": "/llm/json/chunked/api/commands/filter.json",
      "token_estimate": 55
    }
  ]
}