Skip to content

Commit d1cb1ac

Browse files
committed
Merge remote-tracking branch 'origin/dev' into dev
2 parents 6fe6258 + 563ce5d commit d1cb1ac

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

www/content/attributes/hx-include.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@
22
title = "hx-include"
33
+++
44

5-
The `hx-include` attribute allows you to include additional element values in an AJAX request. The value of
6-
this attribute is a CSS query selector of the element or elements to include in the query.
5+
The `hx-include` attribute allows you to include additional element values in an AJAX request. The value of this
6+
attribute can be:
7+
8+
* A CSS query selector of the elements to include.
9+
* `this` which will include the descendants of the element.
10+
* `closest <CSS selector>` which will find the [closest](https://developer.mozilla.org/docs/Web/API/Element/closest)
11+
ancestor element or itself, that matches the given CSS selector
12+
(e.g. `closest tr` will target the closest table row to the element).
13+
* `find <CSS selector>` which will find the first child descendant element that matches the given CSS selector.
14+
* `next <CSS selector>` which will scan the DOM forward for the first element that matches the given CSS selector.
15+
(e.g. `next .error` will target the closest following sibling element with `error` class)
16+
* `previous <CSS selector>` which will scan the DOM backwards for the first element that matches the given CSS selector.
17+
(e.g `previous .error` will target the closest previous sibling with `error` class)
718

819
Here is an example that includes a separate input value:
920

@@ -24,3 +35,19 @@ Note that if you include a non-input element, all input elements enclosed in tha
2435
## Notes
2536

2637
* `hx-include` is inherited and can be placed on a parent element
38+
* While `hx-include` is inherited, it is evaluated from the element triggering the request. It is easy to get confused
39+
when working with the extended selectors such as `find` and `closest`.
40+
```html
41+
<div hx-include="find input">
42+
<button hx-post="/register">
43+
Register!
44+
</button>
45+
Enter email: <input name="email" type="email"/>
46+
</div>
47+
```
48+
In the above example, when clicking on the button, the `find input` selector is resolved from the button itself, which
49+
does not return any element here, since the button doesn't have any `input` child, thus in this case, raises an error.
50+
* A standard CSS selector resolves
51+
to [document.querySelectorAll](https://developer.mozilla.org/docs/Web/API/Document/querySelectorAll) and will include
52+
multiple elements, while the extended selectors such as `find` or `next` only return a single element at most to
53+
include

0 commit comments

Comments
 (0)