Debian Codegraph @

www/src/content/reference/01-attributes/29-hx-status.md

raw symbols SCIP
1 ---
2 title: "hx-status"
3 description: "Handles responses differently by status code"
4 ---
5
6 The `hx-status` attribute overrides swap behavior based on the HTTP response status code.
7
8 ## Examples
9
10 ```html
11 <!-- Show validation errors in a specific container -->
12 <form hx-post="..."
13 hx-status:422="target:#errors select:#validation-errors">
14 ...
15 </form>
16
17 <!-- Suppress swap on server errors -->
18 <button hx-post="..." hx-status:5xx="swap:none">Save</button>
19
20 <!-- Push a custom URL on 201 Created -->
21 <form hx-post="..." hx-status:201="push:/items/new">...</form>
22 ```
23
24 ## Patterns
25
26 The status code goes after the colon: `hx-status:CODE="..."`.
27
28 Supports exact codes, single-digit wildcards (`x`), and range wildcards (`xx`):
29
30 ```html
31 <div hx-get="..."
32 hx-status:404="select:#not-found"
33 hx-status:50x="select:#bad-gateway"
34 hx-status:5xx="swap:none">
35 ...
36 </div>
37 ```
38
39 Evaluated in order of specificity: exact match (`404`), then 2-digit wildcard (`50x`), then 1-digit wildcard (`5xx`).
40
41 ## Modifiers
42
43 The value takes space-separated `key:value` pairs.
44
45 ```html
46 <form hx-post="..."
47 hx-status:422="swap:innerHTML target:#errors select:#validation-errors"
48 hx-status:500="swap:none push:false">
49 ...
50 </form>
51 ```
52
53 ### `swap`
54
55 Swap style for this status code. See [`hx-swap` styles](/reference/attributes/hx-swap#styles) for values.
56
57 ```html
58 <div hx-get="..." hx-status:500="swap:none"></div>
59 ```
60
61 ### `target`
62
63 CSS selector for the swap target. Overrides [`hx-target`](/reference/attributes/hx-target).
64
65 ```html
66 <div hx-get="..." hx-status:422="target:#error-container"></div>
67 ```
68
69 ### `select`
70
71 CSS selector to pick content from the response. Overrides [`hx-select`](/reference/attributes/hx-select).
72
73 ```html
74 <div hx-get="..." hx-status:422="select:#validation-errors"></div>
75 ```
76
77 ### `push`
78
79 Push a URL to browser history. `true`/`false`, or a URL string.
80
81 ```html
82 <div hx-get="..." hx-status:5xx="push:false"></div>
83 <form hx-post="/items" hx-status:201="push:/items/new"></form>
84 ```
85
86 ### `replace`
87
88 Replace the current URL in browser history. `true`/`false`, or a URL string.
89
90 ```html
91 <div hx-get="..." hx-status:301="replace:/new-location"></div>
92 ```
93
94 ### `transition`
95
96 Use the [View Transitions API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transition_API) for this swap. `true`/`false`.
97
98 ```html
99 <div hx-get="..." hx-status:200="transition:true"></div>
100 ```
101
102 ## Notes
103
104 * Values override response headers ([`HX-Retarget`](/reference/headers/HX-Retarget), [`HX-Reswap`](/reference/headers/HX-Reswap), [`HX-Reselect`](/reference/headers/HX-Reselect), etc.)
105 * Without `hx-status`, htmx swaps all responses except [`204`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/204) and [`304`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/304)
106
107 ## See Also
108
109 * [`hx-swap`](/reference/attributes/hx-swap)
110 * [`hx-select`](/reference/attributes/hx-select)
111 * [`hx-target`](/reference/attributes/hx-target)
112 * [`htmx.config.noSwap`](/reference/config/htmx-config-noSwap)