---
id: api/commands/closest
title: closest | Cypress Documentation
description: >-
  Get the first DOM element that matches the selector in Cypress (whether it be
  itself or one of its ancestors).
section: api
source_path: docs/api/commands/closest.mdx
version: 7ada28c0cd90e81cf56fd3fc73de6e6d45c16de6
updated_at: '2026-05-13T21:55:41.935Z'
---
# closest

Get the first DOM element that matches the selector (whether it be itself or one of its ancestors).

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

## Syntax

```
.closest(selector).closest(selector, options)
```

### Usage

**Correct Usage**

```
cy.get('td').closest('.filled') // Yield closest el with class '.filled'
```

**Incorrect Usage**

```
cy.closest('.active') // Errors, cannot be chained off 'cy'cy.clock().closest() // 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 `.closest()`.

| 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 `.closest()` to resolve before [timing out](#Timeouts) |

### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)

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

## Examples

### Selector

#### Find the closest element of the `.error` with the class 'banner'

```
cy.get('p.error').closest('.banner')
```

## Rules

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

*   `.closest()` 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)

*   `.closest()` 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).
*   `.closest()` 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)

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

## Command Log

**_Find the closest element of `li.active` with the class 'nav'_**

```
cy.get('li.active').closest('.nav')
```

The commands above will display in the Command Log as:

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

## See also

*   [`.first()`](/llm/markdown/api/commands/first.md)
*   [`.parent()`](/llm/markdown/api/commands/parent.md)
*   [`.parents()`](/llm/markdown/api/commands/parents.md)
*   [`.parentsUntil()`](/llm/markdown/api/commands/parentsuntil.md)
*   [`.prev()`](/llm/markdown/api/commands/prev.md)
*   [`.prevAll()`](/llm/markdown/api/commands/prevall.md)
*   [`.prevUntil()`](/llm/markdown/api/commands/prevuntil.md)
