Confirmations and Dialogs — HTMX

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…

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>
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

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>

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

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.

What you will learn