Debian Codegraph @

www/src/content/extensions/09-hx-targets.md

raw symbols SCIP
1 ---
2 title: "hx-targets"
3 description: "Swap one response into multiple elements at once"
4 category: "Swap behaviors"
5 icon: "icon-[mdi--target]"
6 keywords: ["targets", "multi-target", "selector", "swap", "broadcast"]
7 ---
8
9 The `targets` extension adds an `hx-targets` attribute that swaps the same response content into multiple elements matching a CSS selector. Useful when you want to update several elements at once with the same response.
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-targets.js"></script>
16 ```
17
18 ## Usage
19
20 Add an `hx-targets` attribute with a CSS selector to any element that makes an htmx request. The response will be swapped into every matching element, using the element's normal [`hx-swap`](/reference/attributes/hx-swap) strategy.
21
22 ```html
23 <button hx-get="/api/notification" hx-targets=".alert-box">
24 Refresh All Alerts
25 </button>
26
27 <div class="alert-box">Alert 1</div>
28 <div class="alert-box">Alert 2</div>
29 <div class="alert-box">Alert 3</div>
30 ```
31
32 When the button is clicked, the response from `/api/notification` is swapped into all three `.alert-box` elements.
33
34 ### Inheritance
35
36 To inherit `hx-targets` from a parent element, use the `hx-targets:inherited` form on the parent:
37
38 ```html
39 <div hx-targets:inherited=".card-body">
40 <button hx-get="/api/content">Update All Cards</button>
41 <button hx-get="/api/reset">Reset All Cards</button>
42
43 <div class="card"><div class="card-body">Card 1</div></div>
44 <div class="card"><div class="card-body">Card 2</div></div>
45 </div>
46 ```
47
48 Both buttons will swap their response into all `.card-body` elements.
49
50 ### Extended Selectors
51
52 The `hx-targets` attribute supports htmx's extended selector syntax, including `closest`, `find`, `next`, and `previous`:
53
54 ```html
55 <div class="container">
56 <button hx-get="/api/status" hx-targets="find .status">
57 Update Statuses
58 </button>
59 <span class="status">OK</span>
60 <span class="status">OK</span>
61 </div>
62 ```
63
64 ### Interaction with hx-target
65
66 When `hx-targets` is present, it overrides [`hx-target`](/reference/attributes/hx-target). The swap style from [`hx-swap`](/reference/attributes/hx-swap) (default: `innerHTML`) is applied to each matched element.
67
68 ```html
69 <button hx-get="/api/time" hx-swap="innerHTML" hx-targets=".clock">
70 Sync Clocks
71 </button>
72
73 <span class="clock">--:--</span>
74 <span class="clock">--:--</span>
75 ```
76
77 ## How It Works
78
79 The extension hooks into the `htmx_before_swap` event. When `hx-targets` is set on the source element:
80
81 1. All elements matching the selector are found
82 2. The main swap task is replaced with one task per matched element
83 3. The response fragment is cloned for each target so every element receives the full response content
84
85 ## Notes
86
87 - If the selector matches zero elements, a warning is logged and the swap proceeds as normal (falling back to [`hx-target`](/reference/attributes/hx-target) or the default target).
88 - Each target receives a clone of the full response. To distribute different parts of the response to different targets, use [`<hx-partial>`](/docs#partials-hx-partial) or [`hx-swap-oob`](/reference/attributes/hx-swap-oob) instead.