# Cypress Documentation
# Source: https://raw.githubusercontent.com/cypress-io/cypress-documentation/main/docs/api/commands/parents.mdx
# Path: docs/api/commands/parents.mdx

---
title: 'parents | Cypress Documentation'
description: Get the parent DOM elements of a set of DOM elements in Cypress.
sidebar_label: parents
slug: /api/commands/parents
---

<ProductHeading product="app" />

# parents

Get the parent DOM elements of a set of DOM elements.

Please note that `.parents()` travels multiple levels up the DOM tree as opposed
to the [.parent ()](/api/commands/parent) command which travels a single level
up the DOM tree.

:::info

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

:::

## Syntax

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

### Usage

<Icon name="check-circle" color="green" /> **Correct Usage**

```javascript
cy.get('aside').parents() // Yield parents of aside
```

<Icon name="exclamation-triangle" color="red" /> **Incorrect Usage**

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

### Arguments

<Icon name="angle-right" /> **selector _(String selector)_**

A selector used to filter matching DOM elements.

<Icon name="angle-right" /> **options _(Object)_**

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

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

<HeaderYields />

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

## Examples

### No Args

#### Get the parents of the active li

```html
<ul class="main-nav">
  <li>Overview</li>
  <li>
    Getting started
    <ul class="sub-nav">
      <li>Install</li>
      <li class="active">Build</li>
      <li>Test</li>
    </ul>
  </li>
</ul>
```

```javascript
// yields [.sub-nav, li, .main-nav]
cy.get('li.active').parents()
```

### Selector

#### Get the parents with class `main-nav` of the active li

```javascript
// yields [.main-nav]
cy.get('li.active').parents('.main-nav')
```

## Rules

<HeaderRequirements />

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

<HeaderAssertions />

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

<HeaderTimeouts />

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

## Command Log

**_Get the parents of the active `li`_**

```javascript
cy.get('li.active').parents()
```

<DocsImage
  src="/img/api/parents/get-all-parents-of-a-dom-element.png"
  alt="Command Log parents"
/>

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

<DocsImage
  src="/img/api/parents/parents-elements-displayed-in-devtools-console.png"
  alt="Console Log parents"
/>

## See also

- [`.children()`](/api/commands/children)
- [`.parent()`](/api/commands/parent)
- [`.parentsUntil()`](/api/commands/parentsuntil)