| 1 | --- |
| 2 | title: "hx-prompt" |
| 3 | description: "Prompt before a request, send the answer as a header" |
| 4 | category: "UX" |
| 5 | icon: "icon-[mdi--comment-question-outline]" |
| 6 | keywords: ["prompt", "input", "header", "hx-prompt", "htmx2"] |
| 7 | --- |
| 8 | |
| 9 | The `hx-prompt` extension restores htmx 2's `hx-prompt` attribute. |
| 10 | |
| 11 | Before each request, it pops up a browser prompt. The answer becomes the `HX-Prompt` header. If the user cancels, the request never fires. |
| 12 | |
| 13 | ## Installing |
| 14 | |
| 15 | ```html |
| 16 | <script src="https://cdn.jsdelivr.net/npm/htmx.org@__VERSION__/dist/htmx.min.js"></script> |
| 17 | <script src="https://cdn.jsdelivr.net/npm/htmx.org@__VERSION__/dist/ext/hx-prompt.js"></script> |
| 18 | ``` |
| 19 | |
| 20 | ## Usage |
| 21 | |
| 22 | ```html |
| 23 | <button hx-delete="/items/1" hx-prompt="Reason for deletion?"> |
| 24 | Delete |
| 25 | </button> |
| 26 | ``` |
| 27 | |
| 28 | On click: |
| 29 | |
| 30 | 1. Browser asks `Reason for deletion?`. |
| 31 | 2. Cancel aborts. |
| 32 | 3. Any answer (even empty) fires a cancelable [`htmx:prompt` event](#htmxprompt-event) carrying it in `event.detail.prompt`. |
| 33 | 4. If nothing cancels it, htmx sends the request with the `HX-Prompt` header set to the answer. |
| 34 | |
| 35 | ### `htmx:prompt` event |
| 36 | |
| 37 | The `htmx:prompt` event fires on the source element after a valid answer. |
| 38 | |
| 39 | Listen with `hx-on::prompt` and call `event.preventDefault()` to abort: |
| 40 | |
| 41 | ```html |
| 42 | <button hx-delete="/items/1" |
| 43 | hx-prompt="Reason?" |
| 44 | hx-on::prompt="if (prompt.length < 3) event.preventDefault()"> |
| 45 | Delete |
| 46 | </button> |
| 47 | ``` |
| 48 | |
| 49 | `event.detail` carries `{ prompt, target }`. `hx-on` exposes it directly, so they're in scope without unwrapping. |
| 50 | |
| 51 | ### Inheritance |
| 52 | |
| 53 | The `hx-prompt` attribute supports inheritance. |
| 54 | |
| 55 | ```html |
| 56 | <div hx-prompt:inherited="Reason?"> |
| 57 | <button hx-delete="/items/1">Delete</button> |
| 58 | <button hx-delete="/items/2">Delete</button> |
| 59 | </div> |
| 60 | ``` |
| 61 | |
| 62 | ### Composing with hx-confirm |
| 63 | |
| 64 | `hx-prompt` runs *before* `hx-confirm`. Both must pass for the request to proceed: |
| 65 | |
| 66 | ```html |
| 67 | <button hx-delete="/items/1" |
| 68 | hx-prompt="Reason?" |
| 69 | hx-confirm="Are you sure?"> |
| 70 | Delete |
| 71 | </button> |
| 72 | ``` |
| 73 | |
| 74 | If the user cancels the prompt, the confirm dialog is not shown. |
| 75 | |
| 76 | ### Custom Dialogs |
| 77 | |
| 78 | Assign a function to `window.htmxPrompt` to use a custom (synchronous) dialog. |
| 79 | |
| 80 | ```js |
| 81 | // receives the question, returns the answer string or null to cancel |
| 82 | window.htmxPrompt = (question) => myCustomSyncDialog(question); |
| 83 | ``` |
| 84 | |
| 85 | The extension uses [`window.prompt`](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt) by default. |
| 86 | |
| 87 | ### Server-Side Example |
| 88 | |
| 89 | `HX-Prompt` is a regular request header. Read it however your server reads headers. |
| 90 | |
| 91 | ```python |
| 92 | def delete_item(request, id): |
| 93 | reason = request.headers.get("HX-Prompt", "") |
| 94 | Item.objects.filter(id=id).delete(reason=reason) |
| 95 | return HttpResponse("") # empty response removes the row |
| 96 | ``` |
| 97 | |
| 98 | ## Without the extension |
| 99 | |
| 100 | If you'd rather skip the extension, a single `hx-on` can do the same thing: |
| 101 | |
| 102 | ```html |
| 103 | <button hx-delete="/items/1" |
| 104 | hx-on::config:request="ctx.request.headers['HX-Prompt'] = prompt('Reason?') ?? event.preventDefault()"> |
| 105 | Delete |
| 106 | </button> |
| 107 | ``` |