| 1 | --- |
| 2 | title: "htmx:before:swap" |
| 3 | description: "Before content is swapped into DOM" |
| 4 | --- |
| 5 | |
| 6 | The `htmx:before:swap` event fires after response content is parsed but before it's inserted or swapped into the DOM. |
| 7 | |
| 8 | ## When It Fires |
| 9 | |
| 10 | After the response is received and parsed, but before any DOM modifications occur. |
| 11 | |
| 12 | ## Event Detail |
| 13 | |
| 14 | - `ctx` - Request context including parsed response |
| 15 | - `tasks` - Array of swap tasks to be performed |
| 16 | |
| 17 | ## Example |
| 18 | |
| 19 | ```javascript |
| 20 | htmx.on('htmx:before:swap', (evt) => { |
| 21 | console.log('About to swap:', evt.detail.ctx.response); |
| 22 | // Modify response before swapping |
| 23 | // Or cancel swap by calling evt.preventDefault() |
| 24 | }); |
| 25 | ``` |
| 26 | |
| 27 | Cancel this event to prevent the swap from occurring. |