---
id: api/commands/focus
title: focus | Cypress Documentation
description: Focus on a DOM element in Cypress.
section: api
source_path: docs/api/commands/focus.mdx
version: ce02913654e2655ee63448bdc92bb92c7b46a619
updated_at: '2026-04-22T19:37:51.587Z'
---
# focus

Focus on a DOM element.

It is [unsafe](/llm/markdown/app/core-concepts/retry-ability.md#Only-queries-are-retried) to
chain further commands that rely on the subject after `.focus()`.

## Syntax

```javascript
.focus()
.focus(options)
```

### Usage

 **Correct Usage**

```javascript
cy.get('input').first().focus() // Focus on the first input
```

 **Incorrect Usage**

```javascript
cy.focus('#search') // Errors, cannot be chained off 'cy'
cy.window().focus() // Errors, 'window' does not yield DOM element
```

### Arguments

 **options *(Object)***

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

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

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

- `.focus()` yields the same subject it was given.
- It is [unsafe](/llm/markdown/app/core-concepts/retry-ability.md#Only-queries-are-retried)
  to chain further commands that rely on the subject after `.focus()`.

## Examples

### No Args

#### Focus on an input

```javascript
cy.get('[type="input"]').focus()
```

#### Focus, type, and blur a textarea

```javascript
cy.get('textarea').focus()
cy.get('textarea').type('Nice Product!')
cy.get('textarea').blur()
```

## Notes

### Actionability

#### Focus is not an action command

`.focus()` is not implemented like other action commands, and does not follow
the same rules of
[waiting for actionability](/llm/markdown/app/core-concepts/interacting-with-elements.md).

`.focus()` is a helpful command used as a shortcut. Normally there's no way for
a user to "focus" an element without causing another action or side effect.
Typically the user would have to click or tab to this element.

Oftentimes using `.focus()` directly is more concise and conveys what you're
trying to test.

If you want the other guarantees of waiting for an element to become actionable,
you should use a different command like [`.click()`](/llm/markdown/api/commands/click.md).

### Blur Events

#### Cypress blurs other focused elements first

If there is currently a different DOM element with focus, Cypress issues a
`blur` event to that element before running the `.focus()` command.

### Focusable

#### Can only be called on a valid focusable element

Ensure the element you are trying to call `.focus()` on is a
[focusable element](https://www.w3.org/TR/html5/editing.html#focusable).

### Timeouts

#### Can time out because your browser did not receive any focus events

If you see this error, you may want to ensure that the main browser window is
currently focused. This means not being focused in debugger or any other window
when the command is run.

Internally Cypress does account for this, and will polyfill the blur events when
necessary to replicate what the browser does. Unfortunately the browser will
still behave differently when not in focus - for instance it may throttle async
events. Your best bet here is to keep Cypress focused when working on a test.

## Rules

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

- `.focus()` requires being chained off a command that yields DOM element(s).
- `.focus()` requires the element to be able to receive focus.

### Assertions [Learn about assertions](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions)

- `.focus()` will automatically wait for assertions you have chained to pass.

### Timeouts [Learn about timeouts](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Timeouts)

- `.focus()` can time out waiting for assertions you've added to pass.

## Command Log

***Focus the textarea***

```javascript
cy.get('[name="comment"]').focus()
```

The commands above will display in the Command Log as:

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

## See also

- [`.blur()`](/llm/markdown/api/commands/blur.md)
- [`.click()`](/llm/markdown/api/commands/click.md)
- [`cy.focused()`](/llm/markdown/api/commands/focused.md)
- [`cy.press()`](/llm/markdown/api/commands/press.md)
- [`.type()`](/llm/markdown/api/commands/type.md)
