Debian Codegraph @

www/src/content/reference/01-attributes/07-hx-swap.md

raw symbols SCIP
1 ---
2 title: "hx-swap"
3 description: "Controls how the response is inserted"
4 ---
5
6 The `hx-swap` attribute controls where the response content goes.
7
8 Defaults to `innerHTML` (configurable via [`htmx.config.defaultSwap`](/reference/config/htmx-config-defaultSwap))
9
10 ## Examples
11
12 ```html
13 <!-- Replace content in container -->
14 <div hx-get="..." hx-swap="innerHTML"></div>
15
16 <!-- Append to list and scroll down -->
17 <div hx-get="..." hx-swap="beforeend scroll:bottom"></div>
18
19 <!-- Update form with smooth transition -->
20 <form hx-post="..." hx-swap="outerHTML transition:true"></form>
21 ```
22
23 ## Swap Methods
24
25 ### `innerHTML`
26
27 Replaces content inside element.
28
29 ```html
30 <div hx-get="..." hx-swap="innerHTML">
31 ... <!-- This gets replaced -->
32 </div>
33 ```
34
35 ### `outerHTML`
36
37 Replaces entire element.
38
39 ```html
40 <!-- This... -->
41 <div hx-get="..." hx-swap="outerHTML">
42 ...
43 </div>
44 <!-- ...gets replaced -->
45 ```
46
47 ### `textContent`
48
49 Replaces the text content of the element, without parsing the response as HTML.
50
51 Useful when the response is plain text and you want to avoid any HTML injection.
52
53 ```html
54 <span hx-get="..." hx-swap="textContent">0</span>
55 ```
56
57 ### `beforebegin` / `before`
58
59 Inserts content before element.
60
61 ```html
62 <!-- Response goes here -->
63 <div hx-get="..." hx-swap="beforebegin">
64 ...
65 </div>
66 ```
67
68 *Can also use `hx-swap="before"`*
69
70 ### `afterbegin` / `prepend`
71
72 Inserts content as first child.
73
74 ```html
75 <div hx-get="..." hx-swap="afterbegin">
76 <!-- Response goes here -->
77 ...
78 </div>
79 ```
80
81 *Can also use `hx-swap="prepend"`*
82
83 ### `beforeend` / `append`
84
85 Inserts content as last child.
86
87 ```html
88 <div hx-get="..." hx-swap="beforeend">
89 ...
90 <!-- Response goes here -->
91 </div>
92 ```
93
94 *Can also use `hx-swap="append"`*
95
96 ### `afterend` / `after`
97
98 Inserts content after element.
99
100 ```html
101 <div hx-get="..." hx-swap="afterend">
102 ...
103 </div>
104 <!-- Response goes here -->
105 ```
106
107 *Can also use `hx-swap="after"`*
108
109 ### `innerMorph`
110
111 Morphs content inside element, preserving state and focus.
112
113 *Uses the [idiomorph](https://github.com/bigskysoftware/idiomorph) algorithm.*
114
115 ```html
116 <div hx-get="..." hx-swap="innerMorph">
117 ... <!-- This gets morphed -->
118 </div>
119 ```
120
121 ### `outerMorph`
122
123 Morphs entire element, preserving state and focus.
124
125 *Uses the [idiomorph](https://github.com/bigskysoftware/idiomorph) algorithm.*
126
127 ```html
128 <!-- This... -->
129 <div hx-get="..." hx-swap="outerMorph">
130 ...
131 </div>
132 <!-- ...gets morphed -->
133 ```
134
135 **Morph exclusions:**
136
137 Add attributes to your server templates to exclude elements from morphing:
138 - `hx-morph-skip` — freeze entire element (attrs + children unchanged)
139 - `hx-morph-skip-children` — freeze children only, attrs still update
140
141 Or configure globally via [`htmx.config.morphSkip`](/reference/config/htmx-config-morphSkip) and [`htmx.config.morphSkipChildren`](/reference/config/htmx-config-morphSkipChildren).
142
143 ### `outerSync`
144
145 Syncs attributes from the response's outer element onto the target, then replaces the target's children. The target element itself stays in the DOM — listeners and component state are preserved.
146
147 Useful when the server returns a full element (e.g. `<section class="active">...</section>`) and you want the target's attributes updated without replacing the element itself.
148
149 ```html
150 <!-- Target keeps its listeners, gets new attrs + children -->
151 <section id="main" hx-get="..." hx-swap="outerSync">
152 ...
153 </section>
154 ```
155
156 `outerHTML` on `document.body` automatically upgrades to `outerSync` so that body attributes (classes, data-attrs) are preserved across full-page swaps.
157
158 ### `delete`
159
160 Removes element (ignores response content).
161
162 ```html
163 <!-- This... -->
164 <div hx-delete="..." hx-swap="delete">
165 ...
166 </div>
167 <!-- ...is removed -->
168 ```
169
170 ### `none`
171
172 Doesn't insert content (out-of-band swaps still work).
173
174 ```html
175 <div hx-get="..." hx-swap="none">
176 <!-- Response not inserted, but OOB swaps happen -->
177 </div>
178 ```
179
180 ### `upsert`
181
182 Updates existing elements by ID and inserts new ones.
183
184 *Requires the [upsert extension](https://github.com/bigskysoftware/htmx-extensions).*
185
186 ```html
187 <div hx-get="..." hx-swap="upsert">
188 <!-- Existing elements with matching IDs are updated, new ones are inserted -->
189 </div>
190 ```
191
192 ## Modifiers
193
194 Customize swap behavior with modifiers.
195
196 ### `transition`
197
198 Enables View Transitions API for smooth page transitions.
199
200 ```html
201 <div hx-swap="innerHTML transition:true"></div>
202 ```
203
204 Enable globally: [`htmx.config.transitions = true`](/reference/config/htmx-config-transitions)
205
206 ### `swap`
207
208 Adds delay before swap.
209
210 Useful for showing loading states or coordinating with CSS animations.
211
212 ```html
213 <div hx-swap="innerHTML swap:1s"></div>
214 ```
215
216 Default: `0ms`
217
218 ### `settle`
219
220 Adds delay between the swap and the settle phase.
221
222 Useful for synchronizing htmx with CSS transition timing.
223
224 ```html
225 <div hx-swap="innerHTML settle:200ms"></div>
226 ```
227
228 Default: `1ms`
229
230 ### `ignoreTitle`
231
232 Prevents updating the page `<title>`.
233
234 By default, htmx updates the page title from `<title>` tags in responses.
235
236 ```html
237 <div hx-swap="innerHTML ignoreTitle:true"></div>
238 ```
239
240 ### `scroll`
241
242 Auto-scroll to swapped content.
243
244 Useful for infinite scroll, chat messages, or focusing attention on new content.
245
246 ```html
247 <div hx-swap="beforeend scroll:bottom"></div>
248 ```
249
250 Values: `top`, `bottom`
251
252 Target a different element:
253
254 ```html
255 <div hx-swap="innerHTML scroll:top scrollTarget:#other"></div>
256 ```
257
258 Scroll the window:
259
260 ```html
261 <div hx-swap="innerHTML scroll:window:top"></div>
262 ```
263
264 ### `show`
265
266 Scrolls to show the target element in viewport.
267
268 Values: `top`, `bottom`, `none`
269
270 ```html
271 <div hx-swap="innerHTML show:top"></div>
272 ```
273
274 Show a different element:
275
276 ```html
277 <div hx-swap="innerHTML show:top showTarget:#other"></div>
278 ```
279
280 Boosted forms default to `show:top`. Disable:
281
282 ```html
283 <form hx-swap="show:none"></form>
284 ```
285
286 ### `target`
287
288 Override swap target inline. Alternative to using [`hx-target`](/reference/attributes/hx-target) attribute.
289
290 ```html
291 <div hx-swap="innerHTML target:#results"></div>
292 ```
293
294 ### `strip`
295
296 Controls whether the outer element of the response content is removed before swapping.
297
298 ```html
299 <div hx-swap="innerHTML strip:true"></div>
300 ```
301
302 ### `swapEmpty`
303
304 Controls whether an empty response body still performs the main swap (which clears the target). `swapEmpty:false` skips the main swap; `swapEmpty:true` (or a bare `swapEmpty`) forces it.
305
306 ```html
307 <!-- Skip the main swap when the response is empty -->
308 <div hx-swap="innerHTML swapEmpty:false"></div>
309
310 <!-- Force the main swap even when the response is empty -->
311 <div hx-swap="innerHTML swapEmpty:true"></div>
312 ```
313
314 Defaults to [`htmx.config.defaultSwapEmpty`](/reference/config/htmx-config-defaultSwapEmpty).
315
316 ## Caveats
317
318 * `outerHTML` on `document.body` automatically upgrades to `outerSync` to preserve body attributes (classes, data-attrs). Use `outerSync` explicitly if you want this behaviour on other elements.