| 1 | --- |
| 2 | title: "htmx.swap()" |
| 3 | description: "Perform an HTML content swap into the DOM" |
| 4 | --- |
| 5 | |
| 6 | The `htmx.swap()` function performs swapping of HTML content into the DOM. This is primarily an internal method used by htmx and extension developers. |
| 7 | |
| 8 | For most use cases, prefer [`htmx.ajax()`](/reference/methods/htmx-ajax) which handles the complete request lifecycle. |
| 9 | |
| 10 | ## Syntax |
| 11 | |
| 12 | ```javascript |
| 13 | htmx.swap(ctx) |
| 14 | ``` |
| 15 | |
| 16 | ## Parameters |
| 17 | |
| 18 | - `ctx` - A context object with the following properties: |
| 19 | - `text` (required) — the HTML content to swap as a string |
| 20 | - `target` — the target element to swap into (defaults to `document.body`) |
| 21 | - `swap` — swap style string (e.g. `'innerHTML'`, `'outerHTML'`) |
| 22 | - `select` — CSS selector to extract content from the response |
| 23 | - `selectOOB` — selector for out-of-band swaps |
| 24 | - `sourceElement` — the element that triggered the swap |
| 25 | - `transition` — boolean, whether to use view transitions |
| 26 | |
| 27 | ## Example |
| 28 | |
| 29 | ```javascript |
| 30 | htmx.swap({ |
| 31 | text: "<div>Swapped!</div>", |
| 32 | target: document.querySelector("#output"), |
| 33 | swap: 'innerHTML' |
| 34 | }); |
| 35 | ``` |