# Conflicts: # docs/source/guides/getting-started/installing-cypress.md
2.5 KiB
title, comments
| title | comments |
|---|---|
| clear | false |
Clear the value of an input or textarea.
{% note info %}
An alias for {% url .type('{selectall}{backspace}') type %}
{% endnote %}
Syntax
.clear()
.clear(options)
Usage
.clear() requires being chained off another cy command that yields an input or textarea.
{% fa fa-check-circle green %} Valid Usage
cy.get('[type="text"]').clear() // Clear text input
cy.get('textarea').type('Hi!').clear() // Clear textarea
cy.focused().clear() // Clear focused input/textarea
{% fa fa-exclamation-triangle red %} Invalid Usage
cy.clear() // Errors, cannot be chained off 'cy'
cy.get('nav').clear() // Errors, 'get' doesn't yield input or textarea
cy.url().clear() // Errors, 'url' doesn't yield DOM element
Arguments
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of .clear().
| Option | Default | Notes |
|---|---|---|
force |
false |
Force clear, disables error checking prior to clear |
interval |
16 |
Interval which to retry clear |
log |
true |
Whether to display command in Command Log |
timeout |
{% url defaultCommandTimeout configuration#Timeouts %} |
Total time to retry the clear |
Yields
.clear() yields the input or textarea that was cleared.
Timeout
.clear() will continue to look for the input or textarea for the duration of the {% url defaultCommandTimeout configuration#Timeouts %}.
Examples
Clear
Clear the input and type a new value.
Prior to clearing, if the element isn't currently focused, Cypress issues a {% url .click() click %} on the element, which causes the element to receive focus.
cy.get('textarea').clear().type('Hello, World')
Command Log
Clear the input and type a new value
cy.get('input[name="name"]').clear().type('Jane Lane')
The commands above will display in the command log as:
When clicking on clear within the command log, the console outputs the following:
See also
- {% url
.blur()blur %} - {% url
.focus()focus %} - {% url
.type()type %}