| 1 | --- |
| 2 | title: "htmx:confirm" |
| 3 | description: "Show confirmation dialog before request" |
| 4 | --- |
| 5 | |
| 6 | The `htmx:confirm` event is fired on every request trigger, allowing custom confirmation dialogs or request cancellation logic. Elements with [`hx-confirm`](/reference/attributes/hx-confirm) use this event to show a confirmation dialog, but the event fires even on elements without `hx-confirm` — it just isn't cancelled by default. |
| 7 | |
| 8 | ## When It Fires |
| 9 | |
| 10 | Before a request is sent, on every triggered request. |
| 11 | |
| 12 | ## Event Detail |
| 13 | |
| 14 | - `ctx` - Request context object |
| 15 | - `issueRequest` - Callback function to call to proceed with the request |
| 16 | - `dropRequest` - Callback function to call to cancel the request |
| 17 | |
| 18 | If you call `evt.preventDefault()`, you **must** call either `issueRequest()` or `dropRequest()`. Failing to call one of them will leave the request pending indefinitely. |
| 19 | |
| 20 | ## Example |
| 21 | |
| 22 | ```javascript |
| 23 | htmx.on('htmx:confirm', (evt) => { |
| 24 | // Ignore elements that don't use hx-confirm |
| 25 | if (!evt.detail.target.hasAttribute('hx-confirm')) return; |
| 26 | |
| 27 | evt.preventDefault(); // Prevent default confirm dialog |
| 28 | |
| 29 | // Show custom modal |
| 30 | showCustomModal(evt.detail.ctx.confirmMessage).then((confirmed) => { |
| 31 | if (confirmed) { |
| 32 | evt.detail.issueRequest(); // User confirmed — proceed |
| 33 | } else { |
| 34 | evt.detail.dropRequest(); // User cancelled — drop the request |
| 35 | } |
| 36 | }); |
| 37 | }); |
| 38 | ``` |
| 39 | |
| 40 | Cancel the default event and call either `issueRequest()` to proceed with the request or `dropRequest()` to cancel it. |