--- title: "hx-targets" description: "Swap one response into multiple elements at once" category: "Swap behaviors" icon: "icon-[mdi--target]" keywords: ["targets", "multi-target", "selector", "swap", "broadcast"] --- 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. ## Installing ```html ``` ## Usage 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. ```html
Alert 1
Alert 2
Alert 3
``` When the button is clicked, the response from `/api/notification` is swapped into all three `.alert-box` elements. ### Inheritance To inherit `hx-targets` from a parent element, use the `hx-targets:inherited` form on the parent: ```html
Card 1
Card 2
``` Both buttons will swap their response into all `.card-body` elements. ### Extended Selectors The `hx-targets` attribute supports htmx's extended selector syntax, including `closest`, `find`, `next`, and `previous`: ```html
OK OK
``` ### Interaction with hx-target 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. ```html --:-- --:-- ``` ## How It Works The extension hooks into the `htmx_before_swap` event. When `hx-targets` is set on the source element: 1. All elements matching the selector are found 2. The main swap task is replaced with one task per matched element 3. The response fragment is cloned for each target so every element receives the full response content ## Notes - 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). - Each target receives a clone of the full response. To distribute different parts of the response to different targets, use [``](/docs#partials-hx-partial) or [`hx-swap-oob`](/reference/attributes/hx-swap-oob) instead.