| 1 | --- |
| 2 | includeMockServer: true |
| 3 | title: "Delete in Place" |
| 4 | description: Remove a record without page refresh |
| 5 | icon: "icon-[mdi--delete]" |
| 6 | soon: true |
| 7 | --- |
| 8 | |
| 9 | <script> |
| 10 | server.get("/demo", () => { |
| 11 | const contacts = [ |
| 12 | { name: "Joe Smith", email: "[email protected]", status: "Active" }, |
| 13 | { name: "Angie MacDowell", email: "[email protected]", status: "Active" }, |
| 14 | { name: "Fuqua Tarkenton", email: "[email protected]", status: "Active" }, |
| 15 | { name: "Kim Yee", email: "[email protected]", status: "Inactive" }, |
| 16 | ]; |
| 17 | return ` class="w-full border-collapse"> |
| 18 | <thead> |
| 19 | <tr> |
| 20 | <th class="text-left px-3 py-2 text-neutral-450 dark:text-neutral-400 font-semibold text-xs uppercase tracking-wide">Name</th> |
| 21 | <th class="text-left px-3 py-2 text-neutral-450 dark:text-neutral-400 font-semibold text-xs uppercase tracking-wide">Email</th> |
| 22 | <th class="text-left px-3 py-2 text-neutral-450 dark:text-neutral-400 font-semibold text-xs uppercase tracking-wide">Status</th> |
| 23 | <th class="text-left px-3 py-2 text-neutral-450 dark:text-neutral-400 font-semibold text-xs uppercase tracking-wide"></th> |
| 24 | </tr> |
| 25 | </thead> |
| 26 | <tbody hx-confirm:inherited="Are you sure?" hx-target:inherited="closest tr" hx-swap:inherited="outerHTML swap:500ms" |
| 27 | class="[&>tr:last-child>td]:border-b-0"> |
| 28 | ${contacts.map((c, i) => rowTemplate(c, i)).join("")} |
| 29 | </tbody> |
| 30 | </table>`; |
| 31 | }); |
| 32 | |
| 33 | server.delete(/\/contact\/\d+/, () => ""); |
| 34 | |
| 35 | function rowTemplate(contact, i) { |
| 36 | return `<tr class="starting:opacity-0 transition-opacity duration-300 ease-out"> |
| 37 | <td class="px-3 py-2.5 border-b border-neutral-100 dark:border-neutral-850 text-sm text-neutral-600 dark:text-neutral-300">${contact.name}</td> |
| 38 | <td class="px-3 py-2.5 border-b border-neutral-100 dark:border-neutral-850 text-sm text-neutral-600 dark:text-neutral-300">${contact.email}</td> |
| 39 | <td class="px-3 py-2.5 border-b border-neutral-100 dark:border-neutral-850 text-sm text-neutral-600 dark:text-neutral-300">${contact.status}</td> |
| 40 | <td class="px-3 py-2.5 border-b border-neutral-100 dark:border-neutral-850 text-sm text-neutral-600 dark:text-neutral-300 text-right"> |
| 41 | <button class="px-2.5 py-1 text-xs font-medium text-neutral-500 dark:text-neutral-400 hover:text-neutral-800 dark:hover:text-neutral-100 rounded cursor-pointer transition-colors" hx-delete="/contact/${i}">Delete</button> |
| 42 | </td> |
| 43 | </tr>`; |
| 44 | } |
| 45 | |
| 46 | server.start("/demo"); |
| 47 | </script> |
| 48 | |
| 49 | <div id="demo-content" class="not-prose demo-container min-h-[280px]"></div> |
| 50 | |
| 51 | ## Basic usage |
| 52 | |
| 53 | On the client, set up the table body with inherited attributes so every delete button shares the same behavior. |
| 54 | |
| 55 | ```html |
| 56 | <tbody hx-confirm:inherited="Are you sure?" |
| 57 | hx-target:inherited="closest tr" |
| 58 | hx-swap:inherited="outerHTML swap:500ms"> |
| 59 | <tr> |
| 60 | <td>Angie MacDowell</td> |
| 61 | <td>[email protected]</td> |
| 62 | <td>Active</td> |
| 63 | <td> |
| 64 | <button hx-delete="/contact/1">Delete</button> |
| 65 | </td> |
| 66 | </tr> |
| 67 | ... |
| 68 | </tbody> |
| 69 | ``` |
| 70 | |
| 71 | - [`hx-confirm`](/reference/attributes/hx-confirm) prompts the user before sending the request. The `:inherited` modifier lets every button in the body inherit it. |
| 72 | - [`hx-target`](/reference/attributes/hx-target)=`"closest tr"` targets the row containing the button. |
| 73 | - [`hx-swap`](/reference/attributes/hx-swap)=`"outerHTML swap:500ms"` replaces the entire row after a 500ms swap delay, giving the fade-out transition time to play. |
| 74 | - [`hx-delete`](/reference/attributes/hx-delete) sends a DELETE request to the server. |
| 75 | |
| 76 | On the server, respond with an empty body and a `200` status. The row is replaced with nothing (it just disappears). |
| 77 | |
| 78 | ## Notes |
| 79 | |
| 80 | ### Fade-out animation |
| 81 | |
| 82 | During the swap delay, htmx adds the `htmx-swapping` class to the target row. Use it to trigger a CSS opacity transition so the row fades out before it's removed. |
| 83 | |
| 84 | ```css |
| 85 | tr.htmx-swapping td { |
| 86 | opacity: 0; |
| 87 | transition: opacity 500ms ease-out; |
| 88 | } |
| 89 | ``` |
| 90 | |
| 91 | <style> |
| 92 | tr.htmx-swapping td { opacity: 0; transition: opacity 500ms ease-out; } |
| 93 | </style> |