Updating Multiple UI Components with htmx Out-of-Band (OOB) Swaps
Learn how to use htmx Out-of-Band (OOB) swaps to update multiple, unrelated DOM elements from a single server response, eliminating the need for multiple AJAX calls.
13 Jul 2025, 14:35 UTC

The Problem: Synchronizing Disparate UI Elements
In a standard htmx request, the server returns a fragment of HTML that replaces a single target element. However, real-world interfaces often require a single action to update multiple, unrelated parts of the page. For example, adding an item to a shopping cart should update the cart count in the header and the item list in the main body simultaneously.
Without a way to target multiple elements, developers typically resort to triggering multiple AJAX requests or implementing a complex client-side state management library. Out-of-Band (OOB) swaps solve this by allowing the server to include additional HTML fragments in a single response that target any element on the page by its ID, regardless of where the original request originated.
Prerequisites
- htmx library loaded in the browser (version 1.0.0 or later).
- A server capable of returning HTML fragments.
- Unique
idattributes assigned to every element intended for an OOB update.
Implementing OOB Swaps
To implement an OOB swap, the server must return a response containing the primary content for the request target, followed by one or more elements marked with the hx-swap-oob attribute.
1. Define the Target Elements
Ensure the elements you wish to update have unique IDs. In this example, we have a main content area and a notification badge in the header.
<!-- Header Component -->
<div id="cart-badge">Items: 0</div>
<!-- Main Content -->
<div id="product-list">
<button hx-post="/add-to-cart/123" hx-target="#product-list">Add to Cart</button>
</div>
2. Construct the Server Response
When the /add-to-cart/123 endpoint is hit, the server should return the updated state for the primary target and the OOB fragment for the badge. The hx-swap-oob="true" attribute tells htmx to move this element out of the primary swap flow and place it into the DOM where the matching ID is found.
<!-- Primary Response: This replaces #product-list -->
<div id="product-list">
<p>Item 123 added successfully!</p>
<button hx-post="/add-to-cart/123" hx-target="#product-list">Add to Cart</button>
</div>
<!-- OOB Response: This replaces #cart-badge -->
<div id="cart-badge" hx-swap-oob="true">Items: 1</div>
3. Advanced Swap Strategies
While hx-swap-oob="true" performs a full replacement of the element, you can specify different swap strategies to append or prepend content without replacing the entire container.
| Value | Behavior | Use Case |
|---|---|---|
true |
Replaces the element with the matching ID. | Updating a counter or a status label. |
beforeend |
Appends content inside the target element. | Adding a new message to a chat log. |
afterbegin |
Prepends content inside the target element. | Adding a new notification to the top of a list. |
Verification and Diagnostics
To verify that OOB swaps are functioning correctly, follow these steps:
- Open the browser Developer Tools > Network tab.
- Trigger the htmx request.
- Inspect the response body. Confirm that it contains both the primary HTML fragment and the element with
hx-swap-oob="true". - Observe the DOM: The primary target should update first, followed immediately by the OOB elements.
Common Failure Points
- Duplicate IDs: If two elements share the same ID, htmx may update the wrong element or fail to update the DOM predictably.
- Incorrect Attribute Placement: The
hx-swap-oobattribute must be on the element that matches the ID of the target in the DOM, not on a wrapper element. - Response Order: While htmx processes OOB swaps after the main content, ensure your server is not sending these fragments as separate HTTP responses, as htmx expects them in a single stream.
Limitations and Trade-offs
OOB swaps are powerful but introduce a coupling between the server's response and the page's global ID structure. If the page layout changes and an ID is renamed or removed, the server may send OOB fragments that have no target, which htmx will simply ignore. To avoid "spaghetti HTML," limit OOB swaps to high-level global components (like navigation bars or notifications) rather than using them for every minor UI update.
Rollback
Because OOB swaps only change the DOM state and do not modify server-side data, the "rollback" consists of refreshing the page to reload the initial server-rendered state. To prevent an accidental OOB update from persisting, ensure your server-side state is updated atomically before returning the HTML response.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.