{
  "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": "ce02913654e2655ee63448bdc92bb92c7b46a619",
    "updated_at": "2026-04-22T19:37:51.587Z",
    "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
      }
    ]
  },
  "content": {
    "type": "root",
    "children": [
      {
        "type": "heading",
        "depth": 1,
        "children": [
          {
            "type": "text",
            "value": "filter"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Get the DOM elements that match a specific selector."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Opposite of "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/not.md",
            "children": [
              {
                "type": "text",
                "value": "`.not()`"
              }
            ]
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The querying behavior of this command matches exactly how\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "http://api.jquery.com/filter",
            "children": [
              {
                "type": "text",
                "value": "`.filter()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " works in jQuery."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Syntax"
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": ".filter(selector)\n.filter(selector, options)"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Usage"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Correct Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "cy.get('td').filter('.users') // Yield all el's with class '.users'"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Incorrect Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "cy.filter('.animated') // Errors, cannot be chained off 'cy'\ncy.clock().filter() // Errors, 'clock' does not yield DOM elements"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Arguments"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "selector (String selector)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "A selector used to filter matching DOM elements."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "options (Object)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Pass in an options object to change the default behavior of `.filter()`."
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Option"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Default"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Description"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`log`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`true`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Displays the command in the "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/open-mode.md#Command-Log",
                    "children": [
                      {
                        "type": "text",
                        "value": "Command log"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`timeout`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/configuration.md#Timeouts",
                    "children": [
                      {
                        "type": "text",
                        "value": "`defaultCommandTimeout`"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Time to wait for `.filter()` to resolve before "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "#Timeouts",
                    "children": [
                      {
                        "type": "text",
                        "value": "timing out"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Yields "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management",
            "children": [
              {
                "type": "text",
                "value": "Learn about subject management"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.filter()` yields the new DOM element(s) it found."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.filter()` is a query, and it is safe to chain further commands."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Examples"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Selector"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Filter the current subject to the elements with the class 'active'"
          }
        ]
      },
      {
        "type": "code",
        "lang": "html",
        "meta": null,
        "value": "<ul>\n  <li>Home</li>\n  <li class=\"active\">About</li>\n  <li>Services</li>\n  <li>Pricing</li>\n  <li>Contact</li>\n</ul>"
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "// yields <li>About</li>\ncy.get('ul').find('>li').filter('.active')"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Contains"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Filter by text"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "You can use the "
          },
          {
            "type": "link",
            "title": null,
            "url": "https://api.jquery.com/contains-selector/",
            "children": [
              {
                "type": "text",
                "value": "jQuery :contains"
              }
            ]
          },
          {
            "type": "text",
            "value": "\nselector to perform a case-sensitive text substring match."
          }
        ]
      },
      {
        "type": "code",
        "lang": "html",
        "meta": null,
        "value": "<ul>\n  <li>Home</li>\n  <li>Services</li>\n  <li>Advanced Services</li>\n  <li>Pricing</li>\n  <li>Contact</li>\n</ul>"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Let's find both list items that contain the work \"Services\""
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "cy.get('li').filter(':contains(\"Services\")').should('have.length', 2)"
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Non-breaking space"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "If the HTML contains a\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "https://en.wikipedia.org/wiki/Non-breaking_space",
            "children": [
              {
                "type": "text",
                "value": "non-breaking space"
              }
            ]
          },
          {
            "type": "text",
            "value": " entity\n`&nbsp;` and the test uses the\n"
          },
          {
            "type": "link",
            "title": null,
            "url": "https://api.jquery.com/contains-selector/",
            "children": [
              {
                "type": "text",
                "value": "jQuery :contains"
              }
            ]
          },
          {
            "type": "text",
            "value": " selector, then the\ntest needs to use the Unicode value `\\u00a0` instead of `&nbsp;`."
          }
        ]
      },
      {
        "type": "code",
        "lang": "html",
        "meta": null,
        "value": "<div data-testid=\"testattr\">\n  <span>Hello&nbsp;world</span>\n</div>"
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "cy.get('[data-testid=\"testattr\"]').filter(':contains(\"Hello\\u00a0world\")')"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Rules"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Requirements "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands",
            "children": [
              {
                "type": "text",
                "value": "Learn about chaining commands"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.filter()` requires being chained off a command that yields DOM element(s)."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Assertions "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions",
            "children": [
              {
                "type": "text",
                "value": "Learn about assertions"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.filter()` will automatically "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/retry-ability.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "retry"
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": "\nuntil the element(s)\n"
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Implicit-Assertions",
                    "children": [
                      {
                        "type": "text",
                        "value": "exist in the DOM"
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": "."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.filter()` will automatically "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/retry-ability.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "retry"
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": "\nuntil all chained assertions have passed."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Timeouts "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Timeouts",
            "children": [
              {
                "type": "text",
                "value": "Learn about timeouts"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.filter()` can time out waiting for the element(s) to\n"
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Implicit-Assertions",
                    "children": [
                      {
                        "type": "text",
                        "value": "exist in the DOM"
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": "."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.filter()` can time out waiting for assertions you've added to pass."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Command Log"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Filter the li's to the li with the class 'active'."
          }
        ]
      },
      {
        "type": "code",
        "lang": "javascript",
        "meta": null,
        "value": "cy.get('.left-nav>.nav').find('>li').filter('.active')"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The commands above will display in the Command Log as:"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "When clicking on the `filter` command within the command log, the console\noutputs the following:"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "See also"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/commands/not.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`.not()`"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "token_estimate": 547
}