htmx Tutorial · User Feedback · Lesson 12 of 30

Confirming Actions: hx-confirm and hx-prompt

Ask before destructive requests with hx-confirm, ask for a value with hx-prompt, and where custom dialogs fit in.

Create a free account to save this page, take notes and track your progress. Sign in

hx-confirm

hx-confirm="message" shows the browser's confirmation dialog before sending the request. If the user cancels, nothing is sent:

Confirm before deletingTry it yourself
<p id="file">report-2026.pdf
  <button hx-delete="/files/report-2026.pdf" hx-confirm="Delete report-2026.pdf? This can't be undone."
          hx-target="#file" hx-swap="outerHTML">Delete</button></p>

<!-- The pretend server: on a real site this part is your backend (Python, PHP, Node, Go...) -->
<script>
  server.delete("/files/:name", (req) => `<p><em>${server.escape(req.params.name)} was deleted.</em></p>`);
</script>

Like most attributes it's inherited, so one hx-confirm on a table can protect every delete button inside.

hx-prompt

hx-prompt="question" asks the user to type something. The answer is sent to the server in the HX-Prompt request header:

Ask for a value with hx-promptTry it yourself
<button hx-post="/rename" hx-prompt="New name for the project?" hx-target="#name">Rename project</button>
<h3 id="name">Untitled project</h3>

<!-- The pretend server: on a real site this part is your backend (Python, PHP, Node, Go...) -->
<script>
  server.post("/rename", (req) => server.escape(req.headers["hx-prompt"] || "Untitled project"));
</script>

Nicer dialogs

The native dialogs work everywhere but can't be styled. To use your own dialog, listen for the htmx:confirm event, call event.preventDefault(), show your dialog, then call event.detail.issueRequest() when the user agrees. Always keep server-side protection too: a confirmation is a convenience, not security.

Test yourself

Where does htmx put the text the user typed into an hx-prompt dialog?

  1. In the HX-Prompt request header
  2. In a form field called prompt
  3. In the URL hash
  4. In localStorage
Show answer

Answer: In the HX-Prompt request header

The server reads the HX-Prompt header.

Was this page helpful?