The cashier means to mark order #14 as ready for pickup, but her finger hits the Delete button instead. Yoga's order is deleted from the database at once, with no question asked. For a button whose result can't be undone, one question before the request is sent is a must.
hx-confirm makes htmx ask first before sending a request, and the request only goes out if the user agrees. Mrs. Rini does the same before she crosses an order out of the book. She asks the cashier, "Delete order #14, Mr. Yoga?". If the cashier says cancel, the book isn't touched at all. Crossing it out is the DELETE request, and Mrs. Rini's question is hx-confirm.
hx-confirm: ask first, then send
<li id="order-14">#14 Mr. Yoga, 5 kg: <span>Ready for pickup</span>
<button hx-delete="/order/14"
hx-confirm="Delete order #14, Mr. Yoga?"
hx-target="closest li"
hx-swap="outerHTML">Delete</button>
</li>
hx-delete="/order/14": a request with theDELETEmethod, which means "delete this data".hx-confirm="...": before the request is made, the browser shows aconfirm()dialog with that text, with OK and Cancel buttons.hx-target="closest li": the target is the nearest<li>row holding the button. In a table the row is a<tr>, soclosest tr.hx-swap="outerHTML": the whole row is replaced by the server's reply. The server replies200with an empty body, so the row disappears.
| The cashier presses | Request | Row #14 |
|---|---|---|
| OK | DELETE /order/14, answered with an empty 200 |
gone |
| Cancel | none | stays |
Cancel means the request is never made: 0 requests, and the server knows nothing.
A 204 reply keeps the row on screen
In Laravel, response()->noContent() replies with status 204 No Content. htmx 2 deliberately swaps nothing for a 204. The order is gone from the database, but its row stays on the board. To remove the row, reply 200 with an empty body: return response('', 200);.
Why the built-in confirm() isn't great
- It stops the whole page. While the dialog is open, the page's JavaScript stops: the board's clock freezes, polling waits, animations stand still. Everything only runs again once the dialog closes.
- It can't be styled. Its look belongs to the browser, and its buttons are just OK and Cancel in the browser's language. You can't write a red "Yes, delete".
- It may not show at all. In an iframe with
sandboxand noallow-modals,confirm()counts as Cancel straight away without showing, so every Delete button does nothing.
Your own dialog: htmx:confirm and dialog
Before every request, htmx fires the htmx:confirm event. If that event is held with preventDefault(), the request waits until you call event.detail.issueRequest(true):
<dialog id="ask-delete">
<p id="ask-text"></p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="yes">Yes, delete</button>
</form>
</dialog>
<script>
document.body.addEventListener('htmx:confirm', (event) => {
if (!event.detail.question) return // requests without hx-confirm run as usual
event.preventDefault() // hold the request
const dialog = document.getElementById('ask-delete')
document.getElementById('ask-text').textContent = event.detail.question
dialog.returnValue = ''
dialog.showModal()
dialog.addEventListener('close', () => {
if (dialog.returnValue === 'yes') event.detail.issueRequest(true)
}, { once: true })
})
</script>
event.detail.questionholds thehx-confirmtext, ornullfor a request withouthx-confirm(the Check status button, the board's polling). Without that check, every request gets held too.- A
<dialog>opened withshowModal()is HTML's built-in dialog: a dark backdrop, focus locked inside it, and Esc closes it. Unlikeconfirm(), it can be styled with CSS, and the page keeps running while it's open. <form method="dialog">: the clicked button closes the dialog, and itsvaluebecomesdialog.returnValue. Esc closes it with an emptyreturnValue, so it counts as cancel.issueRequest(true): send the held request.truemeans skip the built-inconfirm(). Withouttrue, the built-in dialog shows up again after yours, so the cashier is asked twice.
hx-confirm is inherited
hx-confirm applies to the element and everything inside it. Written on <ul id="order-board">, the Check status button inside it also asks "Delete order?". Put it on the deleting button only.
⚠️ Rows that won't go, and dialogs that show up twice
- An empty question.
hx-confirm=""asks nothing. In the exercise, the<button>needs anhx-confirmwith text,hx-delete="/api/users/1"andhx-target="closest tr". - The server replies 204. htmx swaps nothing, so the deleted row stays on screen. Reply with an empty 200. In the exercise, the server's reply is written as
<template data-url="/api/users/1" data-method="delete"></template>, with nodata-status, so its status is 200. - Forgetting to check
event.detail.question. Thehtmx:confirmhandler holds every request, including the ones that need no question. issueRequest()withouttrue. The built-inconfirm()shows up after your dialog.
TL;DR: hx-confirm="Delete order #14, Mr. Yoga?" shows confirm() before the request. OK sends it, Cancel cancels it: the request is never made. With hx-delete, hx-target="closest li" (closest tr in a table) and hx-swap="outerHTML", an empty 200 reply removes the row, while a 204 swaps nothing. confirm() stops the page, can't be styled, and may not show in a sandboxed iframe. Your own dialog: listen for htmx:confirm, let requests without event.detail.question through, preventDefault(), show a <dialog> with showModal(), then call event.detail.issueRequest(true) when the answer is yes. hx-confirm is inherited by everything inside its element.
⚡ hx-confirm: ask first, then send
Write:
<li id="order-14">#14 Mr. Yoga, 5 kg: <span>Ready for pickup</span>
<button hx-delete="/order/14"
hx-confirm="Delete order #14, Mr. Yoga?"
hx-target="closest li"
hx-swap="outerHTML">Delete</button>
</li>
Guess first: the dialog shows up, and the cashier presses Cancel. What happens to the request?
It's cancelled before it's made: 0 requests, the server knows nothing, and row #14 stays.
Meaning: hx-confirm shows confirm() with that text before the request is made, and OK sends it. hx-target="closest li" and hx-swap="outerHTML" replace the whole row with the server's reply.
⚡ Reply with an empty 200, not a 204
Meaning: htmx 2 deliberately swaps nothing for a 204 reply, so the row stays on screen even though the order is gone from the database. Reply 200 with an empty body: return response('', 200);.
⚡ A dialog of your own
Write:
<dialog id="ask-delete">
<p id="ask-text"></p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="yes">Yes, delete</button>
</form>
</dialog>
Meaning: The built-in confirm() stops the page and can't be styled. Listen for htmx:confirm, let requests without event.detail.question through, call preventDefault(), open the dialog with showModal(), then call event.detail.issueRequest(true) when the answer is yes.