| 1 | --- |
| 2 | title: "hx-include" |
| 3 | description: "Includes additional element values in the request" |
| 4 | --- |
| 5 | |
| 6 | The `hx-include` attribute allows you to include additional element values in an AJAX request. |
| 7 | |
| 8 | ## Syntax |
| 9 | |
| 10 | ```html |
| 11 | <button hx-post="/register" hx-include="[name='email']">Register</button> |
| 12 | ``` |
| 13 | |
| 14 | The value of this attribute can be: |
| 15 | |
| 16 | * A CSS query selector of the elements to include. |
| 17 | * `this` which will include the descendants of the element. |
| 18 | * `closest <CSS selector>` which will find the [closest](https://developer.mozilla.org/docs/Web/API/Element/closest) |
| 19 | ancestor element or itself, that matches the given CSS selector |
| 20 | (e.g. `closest tr` will target the closest table row to the element). |
| 21 | * `find <CSS selector>` which will find the first child descendant element that matches the given CSS selector. |
| 22 | * `next <CSS selector>` which will scan the DOM forward for the first element that matches the given CSS selector. |
| 23 | (e.g. `next .error` will target the closest following sibling element with `error` class) |
| 24 | * `previous <CSS selector>` which will scan the DOM backwards for the first element that matches the given CSS selector. |
| 25 | (e.g. `previous .error` will target the closest previous sibling with `error` class) |
| 26 | |
| 27 | Here is an example that includes a separate input value: |
| 28 | |
| 29 | ```html |
| 30 | <div> |
| 31 | <button hx-post="/register" hx-include="[name='email']"> |
| 32 | Register! |
| 33 | </button> |
| 34 | Enter email: <input name="email" type="email"/> |
| 35 | </div> |
| 36 | ``` |
| 37 | |
| 38 | This is a little contrived as you would typically enclose both of these elements in a `form` and submit |
| 39 | the value automatically, but it demonstrates the concept. |
| 40 | |
| 41 | Note that you can also use the `inherit` keyword to inherit parent values for inclusion and add additional values: |
| 42 | |
| 43 | ```html |
| 44 | <main hx-include="#hidden-input"> |
| 45 | ... |
| 46 | <button hx-post="/example" hx-include="inherit, [name='email']"> |
| 47 | Post It! |
| 48 | </button> |
| 49 | Enter email: <input name="email" type="email"/> |
| 50 | </main> |
| 51 | ``` |
| 52 | |
| 53 | Finally, note that if you include a non-input element, all input elements enclosed in that element will be included. |
| 54 | |
| 55 | ## Notes |
| 56 | |
| 57 | when working with the extended selectors such as `find` and `closest`. |
| 58 | |
| 59 | ```html |
| 60 | <div hx-include="find input"> |
| 61 | <button hx-post="/register"> |
| 62 | Register! |
| 63 | </button> |
| 64 | Enter email: <input name="email" type="email"/> |
| 65 | </div> |
| 66 | ``` |
| 67 | |
| 68 | In the above example, when clicking on the button, the `find input` selector is resolved from the button itself, which |
| 69 | does not return any element here, since the button doesn't have any `input` child, thus in this case, raises an error. |
| 70 | |
| 71 | * A standard CSS selector resolves |
| 72 | to [document.querySelectorAll](https://developer.mozilla.org/docs/Web/API/Document/querySelectorAll) and will include |
| 73 | multiple elements, while the extended selectors such as `find` or `next` only return a single element at most to |
| 74 | include |
| 75 | * `hx-include` will ignore disabled inputs |