Debian Codegraph @

www/src/content/reference/01-attributes/08-hx-target.md

raw symbols SCIP
1 ---
2 title: "hx-target"
3 description: "Controls where the response is inserted"
4 ---
5
6 The `hx-target` attribute specifies where to insert the response content.
7
8 Defaults to `this` (which represents the element making the request).
9
10 ## Value
11
12 ### `this`
13
14 Target the element itself.
15
16 ```html
17 <a hx-post="/new-link" hx-target="this" hx-swap="outerHTML">
18 New link
19 </a>
20 ```
21
22 The link updates itself when clicked.
23
24 ### Extended Selectors
25
26 Use any [extended selector](/docs#extended-selectors-1) to target elements:
27
28 * CSS selectors: `#results`, `.container`, `[data-target]`
29 * `closest <selector>` - nearest ancestor (or the element itself) matching selector
30 * `find <selector>` - first child descendant matching selector
31 * `findAll <selector>` - all child descendant elements matching selector
32 * `next` - next sibling element
33 * `next <selector>` - scan forward for selector
34 * `previous` - previous sibling element
35 * `previous <selector>` - scan backward for selector
36
37 See the full [extended selectors](/docs#extended-selectors-1) guide.
38
39 ## Common Patterns
40
41 ### Target a specific element
42
43 ```html
44 <button hx-post="/register" hx-target="#response-div">
45 Register
46 </button>
47 <div id="response-div"></div>
48 ```
49
50 ### Update parent container
51
52 ```html
53 <div class="card">
54 <button hx-get="/refresh" hx-target="closest .card">
55 Refresh Card
56 </button>
57 </div>
58 ```
59
60 ### Update sibling element
61
62 ```html
63 <button hx-get="/data" hx-target="next .results">Load</button>
64 <div class="results"></div>
65 ```
66
67 ### Update child element
68
69 ```html
70 <div hx-get="/user" hx-target="find .status">
71 <span class="status">Loading...</span>
72 </div>
73 ```