Debian Codegraph @

www/src/content/reference/01-attributes/21-hx-sync.md

raw symbols SCIP
1 ---
2 title: "hx-sync"
3 description: "Synchronizes requests between elements"
4 ---
5
6 The `hx-sync` attribute allows you to synchronize AJAX requests between multiple elements.
7
8 ## Syntax
9
10 ```html
11 <input hx-post="/validate" hx-sync="closest form:abort">
12 ```
13
14 The `hx-sync` attribute consists of a CSS selector to indicate the element to synchronize on, followed optionally
15 by a colon and then by an optional syncing strategy. The available strategies are:
16
17 * `drop` - drop (ignore) this request if an existing request is in flight (the default)
18 * `abort` - drop (ignore) this request if an existing request is in flight, and, if that is not the case,
19 *abort* this request if another request occurs while it is still in flight
20 * `replace` - abort the current request, if any, and replace it with this request
21 * `queue` - place this request in the request queue associated with the given element
22
23 The `queue` modifier can take an additional argument indicating exactly how to queue:
24
25 * `queue first` - queue the first request to show up while a request is in flight
26 * `queue last` - queue the last request to show up while a request is in flight
27 * `queue all` - queue all requests that show up while a request is in flight
28
29 ## Notes
30
31 This example resolves a race condition between a form's submit request and an individual input's validation request.
32 Normally, without using `hx-sync`, filling out the input and immediately submitting the form triggers two parallel
33 requests to `/validate` and `/store`. Using `hx-sync="closest form:abort"` on the input will watch for requests on the
34 form and abort the input's request if a form request is present or starts while the input request is in flight.
35
36 ```html
37 <form hx-post="/store">
38 <input id="title" name="title" type="text"
39 hx-post="/validate"
40 hx-trigger="change"
41 hx-sync="closest form:abort">
42 <button type="submit">Submit</button>
43 </form>
44 ```
45
46 If you'd rather prioritize the validation request over the submit request, you can use the `drop` strategy. This example
47 will prioritize the validation request over the submit request so that if a validation request is in flight, the form
48 cannot be submitted.
49
50 ```html
51 <form hx-post="/store">
52 <input id="title" name="title" type="text"
53 hx-post="/validate"
54 hx-trigger="change"
55 hx-sync="closest form:drop"
56 >
57 <button type="submit">Submit</button>
58 </form>
59 ```
60
61 When dealing with forms that contain many inputs, you can prioritize the submit request over all input validation
62 requests using the `hx-sync` `replace` strategy on the form tag. This will cancel any in-flight validation requests and
63 issue only the [`hx-post`](/reference/attributes/hx-post)`="/store"` request. If you'd rather abort the submit request and prioritize any existing
64 validation requests you can use the `hx-sync="this:abort"` strategy on the form tag.
65
66 ```html
67 <form hx-post="/store" hx-sync="this:replace">
68 <input id="title" name="title" type="text" hx-post="/validate" hx-trigger="change" />
69 <button type="submit">Submit</button>
70 </form>
71 ```
72
73 When implementing active search functionality the [`hx-trigger`](/reference/attributes/hx-trigger) attribute's `delay` modifier can be used to debounce the
74 user's input and avoid making multiple requests while the user types. However, once a request is made, if the user
75 begins typing again a new request will begin even if the previous one has not finished processing. This example will
76 cancel any in-flight requests and use only the last request. In cases where the search input is contained within the
77 target, then using `hx-sync` like this also helps reduce the chances that the input will be replaced while the user is
78 still typing.
79
80 ```html
81 <input type="search"
82 hx-get="/search"
83 hx-trigger="keyup changed delay:500ms, search"
84 hx-target="#search-results"
85 hx-sync="this:replace">
86 ```