{
  "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": "5a232a3cf094b8f928b1179ce5af6a5046296fac",
    "updated_at": "2026-11-13T01:14:31.437Z",
    "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)\n.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')\n  .first()\n  .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'\ncy.getCookies().within(() => {}) // Errors, 'getCookies' does not yield DOM element\ncy.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\">\n  The parent div\n  <div class=\"some-child\">Child element</div>\n</div>"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('#within-yields')\n  .within(() => {\n    // we are trying to return something\n    // from the .within callback,\n    // but it won't have any effect\n    return cy.contains('Child element').should('have.class', 'some-child')\n  })\n  .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\">\n  The parent div\n  <div class=\"some-child\">Child element</div>\n</div>"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('#wrap-inside-within')\n  .within(() => {\n    // returning cy.wrap(...) has no effect on the yielded value\n    // it will still be the original parent DOM element\n    return cy.wrap('a new value')\n  })\n  .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>\n  <input name=\"email\" type=\"email\" />\n  <input name=\"password\" type=\"password\" />\n  <button type=\"submit\">Login</button>\n</form>"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('form').within(($form) => {\n  // you have access to the found form via\n  // the jQuery object $form if you need it\n\n  // cy.get() will only search for elements within form,\n  // not within the entire document\n  cy.get('input[name=\"email\"]').type('john.doe@email.com')\n  cy.get('input[name=\"password\"]').type('password')\n  cy.root().submit()\n})"
      },
      {
        "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>\n  <tr>\n    <td>My first client</td>\n    <td>My first project</td>\n    <td>0</td>\n    <td>Active</td>\n    <td><button>Edit</button></td>\n  </tr>\n</table>"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.contains('My first client')\n  .parent('tr')\n  .within(() => {\n    // all searches are automatically rooted to the found tr element\n    cy.get('td').eq(1).contains('My first project')\n    cy.get('td').eq(2).contains('0')\n    cy.get('td').eq(3).contains('Active')\n    cy.get('td').eq(4).contains('button', 'Edit').click()\n  })"
      },
      {
        "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\">\n  {/* note the input field outside the form */}\n  <input id=\"name\" type=\"text\" />\n  <form>\n    <input name=\"email\" type=\"email\" />\n    <input name=\"password\" type=\"password\" />\n    <button type=\"submit\">Login</button>\n  </form>\n</section>"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.get('form').within(($form) => {\n  // temporarily escape the .within context: cy.root() is the <form>, and\n  // .closest('.example') walks up to the ancestor outside it\n  cy.root().closest('.example').find('#name').type('Joe')\n  // continue using the .within context\n  cy.get('input[name=\"email\"]').type('john.doe@email.com')\n  cy.get('input[name=\"password\"]').type('password')\n  cy.root().submit()\n})"
      },
      {
        "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,\n// so chaining `.type()` operates on the wrong (parent) subject.\ncy.get('.ag-header-cell')\n  .first()\n  .within(() => {\n    cy.get('.ag-text-field-input')\n  })\n  .type('hello') // operates on `.ag-header-cell`, not the input\n\n// ✓ Query the inner element directly so it becomes the yielded subject.\ncy.get('.ag-header-cell')\n  .first()\n  // the input is now the yielded subject, so `.type()` operates on it\n  .find('.ag-text-field-input')\n  .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 or the document."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "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) => {\n  cy.get('input:first')\n})"
      },
      {
        "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": 1177
}