Debian Codegraph @

www/src/content/extensions/08-hx-upsert.md

raw symbols SCIP
1 ---
2 title: "hx-upsert"
3 description: "Update or insert elements by ID for dynamic lists"
4 category: "Swap behaviors"
5 icon: "icon-[mdi--update]"
6 keywords: ["upsert", "swap", "list", "update", "insert"]
7 ---
8
9 The `upsert` extension adds a new swap style that intelligently updates existing elements by ID and inserts new ones, while preserving elements not in the response. This is particularly useful for maintaining dynamic lists where you want to update specific items without replacing the entire container.
10
11 ## Installing
12
13 ```html
14 <script src="https://cdn.jsdelivr.net/npm/htmx.org@__VERSION__/dist/htmx.min.js"></script>
15 <script src="https://cdn.jsdelivr.net/npm/htmx.org@__VERSION__/dist/ext/hx-upsert.js"></script>
16 ```
17
18 ## Usage
19
20 Use [`hx-swap`](/reference/attributes/hx-swap)`="upsert"` to apply the upsert behavior:
21
22 ```html
23 <button hx-get="/items" hx-swap="upsert" hx-target="#item-list">
24 Refresh Items
25 </button>
26
27 <div id="item-list">
28 <div id="item-1">Original Item 1</div>
29 <div id="item-2">Original Item 2</div>
30 </div>
31 ```
32
33 When the server responds with:
34
35 ```html
36 <div id="item-2">Updated Item 2</div>
37 <div id="item-3">New Item 3</div>
38 ```
39
40 The result will be:
41
42 ```html
43 <div id="item-list">
44 <div id="item-1">Original Item 1</div>
45 <div id="item-2">Updated Item 2</div>
46 <div id="item-3">New Item 3</div>
47 </div>
48 ```
49
50 ## How It Works
51
52 The upsert swap style:
53
54 1. **Updates** elements with matching IDs (replaces their outerHTML)
55 2. **Inserts** new elements that don't have matching IDs
56 3. **Preserves** existing elements not present in the response
57
58 ## Configuration
59
60 ### Sorting
61
62 Add `sort` to maintain elements in ascending order by ID:
63
64 ```html
65 <div hx-get="/items" hx-swap="upsert sort">
66 ```
67
68 Use `sort:desc` for descending order:
69
70 ```html
71 <div hx-get="/items" hx-swap="upsert sort:desc">
72 ```
73
74 ### Custom Key Attribute
75
76 Use `key:attr` to sort by a different attribute:
77
78 ```html
79 <div hx-get="/items" hx-swap="upsert key:data-priority sort">
80 <div id="task-2" data-priority="1">High Priority</div>
81 <div id="task-1" data-priority="5">Low Priority</div>
82 </div>
83 ```
84
85 ### Prepend Unkeyed Elements
86
87 By default, elements without IDs are appended. Use `prepend` to insert them at the beginning:
88
89 ```html
90 <div hx-get="/items" hx-swap="upsert prepend">
91 ```
92
93 ### Combined Modifiers
94
95 ```html
96 <div hx-get="/items" hx-swap="upsert sort:desc prepend">
97 ```
98
99 ## Using with [`<hx-partial>`](/docs#partials-hx-partial)
100
101 You can use `<hx-partial>` with `hx-swap="upsert"` for targeted upserts in a single response:
102
103 ```html
104 <hx-partial hx-target="#main" hx-swap="innerHTML">
105 <div>Updated main content</div>
106 </hx-partial>
107 <hx-partial hx-target="#item-list" hx-swap="upsert sort">
108 <div id="item-2">Updated Item 2</div>
109 <div id="item-5">New Item 5</div>
110 </hx-partial>
111 ```
112
113 ## Limitations
114
115 - Only elements with `id` attributes can be matched and updated
116 - IDs must be unique across the entire document
117 - Sorting uses numeric-aware `localeCompare`, which may have performance implications for very large lists