{
  "doc": {
    "id": "api/commands/within",
    "title": "cy.within()",
    "description": "Scopes all subsequent Cypress commands to within the element.",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/within.md",
    "version": "e6c8d867c21227247f14714fb5690c7c018983c5",
    "updated_at": "2026-08-08T12:39:37.868Z",
    "headings": [
      {
        "id": "api/commands/within#within",
        "text": "within",
        "level": 1
      },
      {
        "id": "api/commands/within#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/within#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/within#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/within#yields",
        "text": "Yields",
        "level": 3
      },
      {
        "id": "api/commands/within#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/within#forms",
        "text": "Forms",
        "level": 3
      },
      {
        "id": "api/commands/within#get-inputs-within-a-form-and-submit-the-form",
        "text": "Get inputs within a form and submit the form",
        "level": 4
      },
      {
        "id": "api/commands/within#tables",
        "text": "Tables",
        "level": 3
      },
      {
        "id": "api/commands/within#find-row-with-specific-cell-and-confirm-other-cells-in-the-row",
        "text": "Find row with specific cell and confirm other cells in the row",
        "level": 4
      },
      {
        "id": "api/commands/within#temporarily-escape",
        "text": "Temporarily escape",
        "level": 3
      },
      {
        "id": "api/commands/within#notes",
        "text": "Notes",
        "level": 2
      },
      {
        "id": "api/commands/within#yielded-subject",
        "text": "Yielded Subject",
        "level": 3
      },
      {
        "id": "api/commands/within#within-cannot-be-used-to-return-an-inner-element",
        "text": ".within() cannot be used to return an inner element",
        "level": 4
      },
      {
        "id": "api/commands/within#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/within#requirements",
        "text": "Requirements",
        "level": 3
      },
      {
        "id": "api/commands/within#assertions",
        "text": "Assertions",
        "level": 3
      },
      {
        "id": "api/commands/within#timeouts",
        "text": "Timeouts",
        "level": 3
      },
      {
        "id": "api/commands/within#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/within#history",
        "text": "History",
        "level": 2
      },
      {
        "id": "api/commands/within#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "content": {
    "type": "root",
    "children": [
      {
        "type": "heading",
        "depth": 1,
        "children": [
          {
            "type": "text",
            "value": "within"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Scopes all subsequent Cypress commands to within the element. Useful when working within a particular group of elements such as a `<form>`."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "It is "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/retry-ability.md#Only-queries-are-retried",
            "children": [
              {
                "type": "text",
                "value": "unsafe"
              }
            ]
          },
          {
            "type": "text",
            "value": " to chain further commands that rely on the subject after `.within()`."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Syntax"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": ".within(callbackFn).within(options, callbackFn)"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Usage"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Correct Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('.list')  .first()  .within(($list) => {}) // Yield the first `.list` and scope all commands within it"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Incorrect Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.within(() => {}) // Errors, cannot be chained off 'cy'cy.getCookies().within(() => {}) // Errors, 'getCookies' does not yield DOM elementcy.get('div').within(($divs) => {}) // Probably errors, because get('div') yields multiple elements"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Arguments"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "callbackFn (Function)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Pass a function that takes the current yielded subject as its first argument."
          }
        ]
      },
      {
        "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 `.within()`."
          }
        ]
      },
      {
        "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": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Yields"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.within()` yields the same subject it was given."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "It is "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/retry-ability.md#Only-queries-are-retried",
                    "children": [
                      {
                        "type": "text",
                        "value": "unsafe"
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": " to chain further commands that rely on the subject after `.within()`."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Trying to return a different element the `.within` callback have no effect:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "<div id=\"within-yields\">  The parent div  <div class=\"some-child\">Child element</div></div>"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('#within-yields')  .within(() => {    // we are trying to return something    // from the .within callback,    // but it won't have any effect    return cy.contains('Child element').should('have.class', 'some-child')  })  .should('have.id', 'within-yields')"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Similarly, trying to change the subject by using the "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/wrap.md",
            "children": [
              {
                "type": "text",
                "value": "cy.wrap"
              }
            ]
          },
          {
            "type": "text",
            "value": " command inside the `.within` callback have no effect:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "<div id=\"wrap-inside-within\">  The parent div  <div class=\"some-child\">Child element</div></div>"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('#wrap-inside-within')  .within(() => {    // returning cy.wrap(...) has no effect on the yielded value    // it will still be the original parent DOM element    return cy.wrap('a new value')  })  .should('have.id', 'wrap-inside-within')"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Examples"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Forms"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Get inputs within a form and submit the form"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "<form>  <input name=\"email\" type=\"email\" />  <input name=\"password\" type=\"password\" />  <button type=\"submit\">Login</button></form>"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('form').within(($form) => {  // you have access to the found form via  // the jQuery object $form if you need it  // cy.get() will only search for elements within form,  // not within the entire document  cy.get('input[name=\"email\"]').type('john.doe@email.com')  cy.get('input[name=\"password\"]').type('password')  cy.root().submit()})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Tables"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Find row with specific cell and confirm other cells in the row"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "<table>  <tr>    <td>My first client</td>    <td>My first project</td>    <td>0</td>    <td>Active</td>    <td><button>Edit</button></td>  </tr></table>"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.contains('My first client')  .parent('tr')  .within(() => {    // all searches are automatically rooted to the found tr element    cy.get('td').eq(1).contains('My first project')    cy.get('td').eq(2).contains('0')    cy.get('td').eq(3).contains('Active')    cy.get('td').eq(4).contains('button', 'Edit').click()  })"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Temporarily escape"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "You can temporarily escape the `.within` context by starting a new command chain with "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/root.md",
            "children": [
              {
                "type": "text",
                "value": "cy.root"
              }
            ]
          },
          {
            "type": "text",
            "value": " followed by "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/closest.md",
            "children": [
              {
                "type": "text",
                "value": ".closest"
              }
            ]
          },
          {
            "type": "text",
            "value": ". Inside a `.within()` block, `cy.root()` yields the scoped element (here, the `<form>`) rather than `<html>`. Chaining "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/closest.md",
            "children": [
              {
                "type": "text",
                "value": ".closest"
              }
            ]
          },
          {
            "type": "text",
            "value": " then traverses up from that element to a shared ancestor outside the scope, from which you can query elements that live outside the `.within()` context."
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "<section class=\"example\">  {/* note the input field outside the form */}  <input id=\"name\" type=\"text\" />  <form>    <input name=\"email\" type=\"email\" />    <input name=\"password\" type=\"password\" />    <button type=\"submit\">Login</button>  </form></section>"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('form').within(($form) => {  // temporarily escape the .within context: cy.root() is the <form>, and  // .closest('.example') walks up to the ancestor outside it  cy.root().closest('.example').find('#name').type('Joe')  // continue using the .within context  cy.get('input[name=\"email\"]').type('john.doe@email.com')  cy.get('input[name=\"password\"]').type('password')  cy.root().submit()})"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Notes"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Yielded Subject"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "`.within()` cannot be used to return an inner element"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The subject yielded by `.within()` is always the original parent element. Any command you run, value you return, or "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/wrap.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.wrap()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " you call inside the callback has no effect on what `.within()` yields. This is true even when `.within()` is used inside a "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/cypress-api/custom-commands.md",
            "children": [
              {
                "type": "text",
                "value": "custom command"
              }
            ]
          },
          {
            "type": "text",
            "value": " or when `.within()` blocks are nested."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "If you need to yield an element found inside the scope, query it after the `.within()` block instead. For example, scope to the parent with "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/find.md",
            "children": [
              {
                "type": "text",
                "value": "`.find()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " (or chain queries off the parent directly) rather than relying on `.within()` to hand back the inner element:"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// ✗ The inner `cy.get()` is NOT yielded — `.within()` yields the parent,// so chaining `.type()` operates on the wrong (parent) subject.cy.get('.ag-header-cell')  .first()  .within(() => {    cy.get('.ag-text-field-input')  })  .type('hello') // operates on `.ag-header-cell`, not the input// ✓ Query the inner element directly so it becomes the yielded subject.cy.get('.ag-header-cell')  .first()  // the input is now the yielded subject, so `.type()` operates on it  .find('.ag-text-field-input')  .type('hello')"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Rules"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Requirements"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.within()` requires being chained off a previous command that yields exactly one DOM element."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Assertions"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.within()` will only run assertions you have chained once, and will not "
                  },
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/retry-ability.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "retry"
                      }
                    ]
                  },
                  {
                    "type": "text",
                    "value": "."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Timeouts"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.within()` cannot time out."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Command Log"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Get the input within the form"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('.query-form').within((el) => {  cy.get('input:first')})"
      },
      {
        "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 `within` command within the command log, the console outputs the following:"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "History"
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Version"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Changes"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/changelog.md#12-0-0",
                    "children": [
                      {
                        "type": "text",
                        "value": "12.0.0"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`.within()` now throws an error when given more than one element as the subject."
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/changelog.md#5-4-0",
                    "children": [
                      {
                        "type": "text",
                        "value": "5.4.0"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "fixed the yielded value to always be the parent element"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/changelog.md#0-3-3",
                    "children": [
                      {
                        "type": "text",
                        "value": "< 0.3.3"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`.within()` command added"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "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/root.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`.root()`"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "token_estimate": 1153
}