Multi-Target Swaps

Update multiple elements from a single response
Two patterns for multi-target updates:
multi-target put htmx 4 partials OOB updates

Demo 1: Explicit Multi-Swap

Several put commands update different parts of the page:

Main Content

Welcome

Click the button to update all panels

Statistics

Stats

0

Sidebar

Notifications

No new notifications

Waiting...

Demo 2: Partials

One response carries several fragments, each naming its target:

How it works: a partials response carries a fragment per target. This demo performs those swaps explicitly; hyperfixi 3.x's process partials command did it for you and retired in 4.0. With htmx 4 loaded beside hyperfixi, the server returns <template hx type="partial" hx-target="#id"> elements (or hx-swap-oob) and htmx distributes them.

Main (partial)

Waiting for partial...

Click the button above

Counter (partial)

Counter

?

Alerts (beforeEnd strategy)

Alerts will append here...

Demo 3: Dashboard Refresh (Simulated API)

Simulates fetching a dashboard response with multiple components:

Users

Active Users

---

Revenue

Revenue

$---

Orders

Orders

---

Activity Log

Activity will appear here...

The Code

Explicit Multi-Swap:

on click fetch "/api/dashboard" as json put it.data into data -- Update multiple targets put data.main into #main put data.sidebar into #sidebar put data.notifications into #notifications

htmx 4 Partials Response:

<template hx type="partial" hx-target="#main"> <!-- Content for #main --> <div>Main content here</div> </template> <template hx type="partial" id="sidebar"> <!-- No hx-target: the template's id names the target --> <nav>Sidebar content</nav> </template> <template hx type="partial" hx-target="#notifications" hx-swap="beforeend"> <!-- Append to notifications --> <div class="toast">New message!</div> </template>

hyperfixi 3.x distributed <hx-partial> elements with a process partials command, which retired with core's htmx layer in 4.0. For server-driven multi-target responses, load htmx 4 beside hyperfixi and return the templates above (or elements with hx-swap-oob); the explicit updates above are the hyperscript-only approach.

How it works:

Partial Attributes (htmx 4):

Attribute Description
hx-target CSS selector for the target element (defaults to the template's id)
hx-swap Swap style (innerHTML, beforeend, etc.; defaults to htmx's default swap)

htmx Comparison:

htmx 4 (in the response) hyperscript (in the page)
<div id="x" hx-swap-oob="true"> put data.x into #x
<template hx type="partial" hx-target="#x" hx-swap="beforeend"> put data.x at end of #x

Use Cases: