| 1 | --- |
| 2 | includeMockServer: true |
| 3 | title: "Edit in Place" |
| 4 | description: Update a record without page refresh |
| 5 | icon: "icon-[mdi--square-edit-outline]" |
| 6 | soon: true |
| 7 | --- |
| 8 | |
| 9 | <script> |
| 10 | var _user = { name: "Joe Smith", email: "[email protected]" }; |
| 11 | |
| 12 | function viewHTML() { |
| 13 | return ` |
| 14 | <div id="user-view" class="p-5 border border-neutral-200 dark:border-neutral-800 rounded-lg max-w-sm"> |
| 15 | <div class="mb-3"> |
| 16 | <span class="block text-xs font-medium uppercase tracking-wide text-neutral-500 dark:text-neutral-400 mb-1">Name</span> |
| 17 | <span class="text-sm font-medium text-neutral-800 dark:text-neutral-100">${_user.name}</span> |
| 18 | </div> |
| 19 | <div class="mb-4"> |
| 20 | <span class="block text-xs font-medium uppercase tracking-wide text-neutral-500 dark:text-neutral-400 mb-1">Email</span> |
| 21 | <span class="text-sm font-medium text-neutral-800 dark:text-neutral-100">${_user.email}</span> |
| 22 | </div> |
| 23 | <button class="px-3.5 py-1.5 text-sm font-medium rounded-md cursor-pointer border border-neutral-200 dark:border-neutral-700 text-neutral-600 dark:text-neutral-300 interact:bg-neutral-50 dark:interact:bg-neutral-850 active:scale-[0.98] transition" hx-get="/users/1/edit" hx-target="#user-view" hx-swap="outerHTML">Edit</button> |
| 24 | </div>`; |
| 25 | } |
| 26 | |
| 27 | server.get("/users/1", () => viewHTML()); |
| 28 | |
| 29 | server.get("/users/1/edit", () => ` |
| 30 | <form id="user-view" class="p-5 border border-neutral-200 dark:border-neutral-800 rounded-lg max-w-sm" hx-put="/users/1" hx-target="this" hx-swap="outerHTML"> |
| 31 | <div class="mb-3"> |
| 32 | <label class="block text-xs font-medium uppercase tracking-wide text-neutral-500 dark:text-neutral-400 mb-1">Name |
| 33 | <input class="mt-1 w-full px-2.5 py-1.5 text-sm border border-neutral-300 dark:border-neutral-700 rounded-md bg-white dark:bg-neutral-900 text-neutral-800 dark:text-neutral-100 focus:outline-none focus:ring-2 focus:ring-neutral-400/30 focus:border-neutral-400 dark:focus:border-neutral-500" name="name" value="${_user.name}" autofocus> |
| 34 | </label> |
| 35 | </div> |
| 36 | <div class="mb-4"> |
| 37 | <label class="block text-xs font-medium uppercase tracking-wide text-neutral-500 dark:text-neutral-400 mb-1">Email |
| 38 | <input class="mt-1 w-full px-2.5 py-1.5 text-sm border border-neutral-300 dark:border-neutral-700 rounded-md bg-white dark:bg-neutral-900 text-neutral-800 dark:text-neutral-100 focus:outline-none focus:ring-2 focus:ring-neutral-400/30 focus:border-neutral-400 dark:focus:border-neutral-500" name="email" value="${_user.email}" type="email"> |
| 39 | </label> |
| 40 | </div> |
| 41 | <div class="flex gap-2"> |
| 42 | <button class="px-3.5 py-1.5 text-sm font-medium rounded-md cursor-pointer text-white dark:text-neutral-900 bg-neutral-800 dark:bg-neutral-200 interact:bg-neutral-700 dark:interact:bg-neutral-300 active:scale-[0.98] transition" type="submit">Save</button> |
| 43 | <button class="px-3.5 py-1.5 text-sm font-medium rounded-md cursor-pointer bg-neutral-100 dark:bg-neutral-800 text-neutral-700 dark:text-neutral-300 interact:bg-neutral-200 dark:interact:bg-neutral-700 active:scale-[0.98] transition" type="button" hx-get="/users/1">Cancel</button> |
| 44 | </div> |
| 45 | </form>`); |
| 46 | |
| 47 | server.put("/users/1", (req) => { |
| 48 | _user.name = req.params.name; |
| 49 | _user.email = req.params.email; |
| 50 | return viewHTML(); |
| 51 | }); |
| 52 | |
| 53 | server.start("/users/1"); |
| 54 | </script> |
| 55 | |
| 56 | <div id="demo-content" class="not-prose demo-container flex justify-center"></div> |
| 57 | |
| 58 | ## Basic usage |
| 59 | |
| 60 | On the client, display the current values with a button that fetches the edit form. |
| 61 | |
| 62 | ```html |
| 63 | <div hx-target="this" hx-swap="outerHTML"> |
| 64 | <p>Name: Joe Smith</p> |
| 65 | <p>Email: [email protected]</p> |
| 66 | <button hx-get="/users/1/edit">Edit</button> |
| 67 | </div> |
| 68 | ``` |
| 69 | |
| 70 | - [`hx-target`](/reference/attributes/hx-target)=[`"this"`](/reference/attributes/hx-target#this) sets the target to the `<div>` itself. |
| 71 | - [`hx-swap`](/reference/attributes/hx-swap)=[`"outerHTML"`](/reference/attributes/hx-swap#outerhtml) replaces the entire `<div>` with the response. |
| 72 | - [`hx-get`](/reference/attributes/hx-get) on the button requests the edit form. |
| 73 | |
| 74 | On the server, respond with a form pre-filled with the current values. |
| 75 | |
| 76 | ```html |
| 77 | <form hx-put="/users/1" hx-target="this" hx-swap="outerHTML"> |
| 78 | <label>Name <input name="name" value="Joe Smith"></label> |
| 79 | <label>Email <input name="email" value="[email protected]"></label> |
| 80 | <button type="submit">Save</button> |
| 81 | <button type="button" hx-get="/users/1">Cancel</button> |
| 82 | </form> |
| 83 | ``` |
| 84 | |
| 85 | - [`hx-put`](/reference/attributes/hx-put) submits the form as a `PUT` request. |
| 86 | - **Save** submits the form, and the server responds with the updated view mode HTML. |
| 87 | - **Cancel** re-fetches the view mode without saving. |
| 88 | |
| 89 | ## Notes |
| 90 | |
| 91 | ### REST conventions |
| 92 | |
| 93 | The endpoints follow standard REST conventions: |
| 94 | |
| 95 | - `GET /users/1`: retrieve the view |
| 96 | - `GET /users/1/edit`: retrieve the edit form |
| 97 | - `PUT /users/1`: update the resource |
| 98 | |
| 99 | The URL represents the resource, and the HTTP method indicates the action. |