| 1 | --- |
| 2 | title: "hx-swap-oob" |
| 3 | description: "Marks response elements to swap into the page by ID" |
| 4 | --- |
| 5 | |
| 6 | The `hx-swap-oob` attribute allows you to specify that some content in a response should be |
| 7 | swapped into the DOM somewhere other than the target, that is "Out of Band". This allows you to piggyback updates to |
| 8 | other element updates on a response. |
| 9 | |
| 10 | ## Syntax |
| 11 | |
| 12 | ```html |
| 13 | <div id="alerts" hx-swap-oob="true"> |
| 14 | Alert message |
| 15 | </div> |
| 16 | ``` |
| 17 | |
| 18 | Consider the following response HTML: |
| 19 | |
| 20 | ```html |
| 21 | <div> |
| 22 | ... |
| 23 | </div> |
| 24 | <div id="alerts" hx-swap-oob="true"> |
| 25 | Saved! |
| 26 | </div> |
| 27 | |
| 28 | ``` |
| 29 | |
| 30 | The first div will be swapped into the target the usual manner. The second div, however, will be swapped in as a |
| 31 | replacement for the element with the id `alerts`, and will not end up in the target. |
| 32 | |
| 33 | The value of the `hx-swap-oob` can be: |
| 34 | |
| 35 | * `true` |
| 36 | * any valid [`hx-swap`](/reference/attributes/hx-swap) value |
| 37 | * any valid [`hx-swap`](/reference/attributes/hx-swap) value, followed by a colon, followed by a CSS selector |
| 38 | |
| 39 | If the value is `true` or `outerHTML` (which are equivalent) the element will be swapped inline. |
| 40 | |
| 41 | If a swap value is given, that swap strategy will be used and the encapsulating tag pair will be stripped for all |
| 42 | strategies other than `outerHTML`. |
| 43 | |
| 44 | If a selector is given, all elements matched by that selector will be swapped. If not, the element with an ID matching |
| 45 | the new content will be swapped. |
| 46 | |
| 47 | ### Using alternate swap strategies |
| 48 | |
| 49 | As mentioned previously when using swap strategies other than `true` or `outerHTML` the encapsulating tags are stripped, |
| 50 | as such you need to encapsulate the returned data with the correct tags for the context. |
| 51 | |
| 52 | When trying to insert a `<tr>` in a table that uses `<tbody>`: |
| 53 | |
| 54 | ```html |
| 55 | <tbody hx-swap-oob="beforeend:#table tbody"> |
| 56 | <tr> |
| 57 | ... |
| 58 | </tr> |
| 59 | </tbody> |
| 60 | ``` |
| 61 | |
| 62 | A "plain" table: |
| 63 | |
| 64 | ```html |
| 65 | <table hx-swap-oob="beforeend:#table2"> |
| 66 | <tr> |
| 67 | ... |
| 68 | </tr> |
| 69 | </table> |
| 70 | ``` |
| 71 | |
| 72 | A `<li>` may be encapsulated in `<ul>`, `<ol>`, `<div>` or `<span>`, for example: |
| 73 | |
| 74 | ```html |
| 75 | <ul hx-swap-oob="beforeend:#list1"> |
| 76 | <li>...</li> |
| 77 | </ul> |
| 78 | ``` |
| 79 | |
| 80 | A `<p>` can be encapsulated in `<div>` or `<span>`: |
| 81 | |
| 82 | ```html |
| 83 | <span hx-swap-oob="beforeend:#text"> |
| 84 | <p>...</p> |
| 85 | </strong> |
| 86 | ``` |
| 87 | |
| 88 | ### Troublesome Tables and lists |
| 89 | |
| 90 | Note that you can use a `template` tag to encapsulate types of elements that, by the HTML spec, can't stand on their own |
| 91 | in the |
| 92 | DOM (`<tr>`, `<td>`, `<th>`, `<thead>`, `<tbody>`, `<tfoot>`, `<colgroup>`, `<caption>`, `<col>` & `<li>`). |
| 93 | |
| 94 | Here is an example with an out-of-band swap of a table row being encapsulated in this way: |
| 95 | |
| 96 | ```html |
| 97 | <div> |
| 98 | ... |
| 99 | </div> |
| 100 | <template> |
| 101 | <tr id="row" hx-swap-oob="true"> |
| 102 | ... |
| 103 | </tr> |
| 104 | </template> |
| 105 | ``` |
| 106 | |
| 107 | Note that these template tags will be removed from the final content of the page. |
| 108 | |
| 109 | ### Slippery SVGs |
| 110 | |
| 111 | Some element types, like SVG, use a specific XML namespace for their child elements. This prevents internal elements |
| 112 | from working correctly when swapped in, unless they are encapsulated within a `svg` tag. To modify the internal contents |
| 113 | of an existing SVG, you can use both `template` and `svg` tags to encapsulate the elements, allowing them to get the |
| 114 | correct namespace applied. |
| 115 | |
| 116 | Here is an example with an out-of-band swap of svg elements being encapsulated in this way: |
| 117 | |
| 118 | ```html |
| 119 | <div> |
| 120 | ... |
| 121 | </div> |
| 122 | <template><svg> |
| 123 | <circle hx-swap-oob="true" id="circle1" r="35" cx="50" cy="50" fill="red" /> |
| 124 | </svg></template> |
| 125 | <template><svg hx-swap-oob="beforebegin:#circle1"> |
| 126 | <circle id="circle2" r="45" cx="50" cy="50" fill="blue" /> |
| 127 | </svg></template> |
| 128 | ``` |
| 129 | |
| 130 | This will replace circle1 inline and then insert circle2 before circle1. |
| 131 | |
| 132 | Note that these `template` and `svg` wrapping tags will be removed from the final content of the page. |
| 133 | |
| 134 | ## Nested OOB Swaps |
| 135 | |
| 136 | By default, any element with `hx-swap-oob=` attribute anywhere in the response is processed for oob swap behavior, |
| 137 | including when an element is nested within the main response element. |
| 138 | This can be problematic when using [template fragments](https://htmx.org/essays/template-fragments/) where a fragment |
| 139 | may be reused as an oob-swap target and also as part of a bigger fragment. When the bigger fragment is the main response |
| 140 | the inner fragment will still be processed as an oob swap, removing it from the dom. |
| 141 | |
| 142 | This behavior can be changed by setting the config `htmx.config.allowNestedOobSwaps` to `false`. If this config option |
| 143 | is `false`, OOB swaps are only processed when the element is *adjacent to* the main response element, OOB swaps |
| 144 | elsewhere will be ignored and oob-swap-related attributes stripped. |
| 145 | |
| 146 | ## Empty Response Behaviour |
| 147 | |
| 148 | When a response contains only `hx-swap-oob` elements and no main content, htmx still performs the main swap with an |
| 149 | empty fragment. You might for example submit a form, return an empty response to remove the form |
| 150 | from the page, and include `hx-swap-oob` elements to place the result elsewhere. |
| 151 | |
| 152 | ```html |
| 153 | <!-- Server returns this to remove the form and update the list --> |
| 154 | <div id="item-list" hx-swap-oob="beforeend"> |
| 155 | <li>New item</li> |
| 156 | </div> |
| 157 | <!-- no main content = form gets swapped with empty fragment, removing it --> |
| 158 | ``` |
| 159 | |
| 160 | If you want to prevent the empty main swap, use the [`swapEmpty`](/reference/attributes/hx-swap#swapempty) modifier: |
| 161 | |
| 162 | ```html |
| 163 | <form hx-post="/submit" hx-swap="outerHTML swapEmpty:false"> |
| 164 | ``` |
| 165 | |
| 166 | Or set the global default via [`htmx.config.defaultSwapEmpty`](/reference/config/htmx-config-defaultSwapEmpty). |
| 167 | |
| 168 | [`<hx-partial>`](/docs#partials-hx-partial) has the opposite default: a response containing only `<hx-partial>` elements |
| 169 | and no main content will **not** trigger an empty main swap. This is because `<hx-partial>`-only responses signal |
| 170 | intent — the server is explicitly routing multiple targeted updates with no main content to swap. Use `swapEmpty:true` on |
| 171 | the triggering element if you need the main swap to run anyway. |
| 172 | |
| 173 | ## See Also |
| 174 | |
| 175 | - [`<hx-partial>`](/docs#partials-hx-partial), an alternative for multi-target updates with explicit control over targeting and swap strategy |