mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-09-28 05:21:18 +00:00
2.7 KiB
2.7 KiB
layout | title |
---|---|
layout.njk | </> htmx - hx-inherit |
hx-inherit
The hx-inherit
attribute allows you to control the automatic attribute inheritance of an element to its
children nodes. One use case here is, to allow wrapping the whole page in hx-boost
, while not having
the configuration of that wrapper affect a more elaborated child node htmx configuration.
The default behavior for htmx is to inherit all attributes automatically.
hx-inherit
disables inheritance.
htmx evaluates inheritance as follows:
- when
hx-boost
is not set- get all elements that defines a
hx-<verb>
(hx-get
,hx-post
,hx-patch
,hx-put
orhx-delete
) - for each htmx attribute, either find the htmx attribute on the element the
hx-<verb>
was defined on or traverse up all the parent nodes - the first element's value the corresponding htmx attribute has been found on is then used
- get all elements that defines a
- when
hx-boost
is set- make all
<a>
tags ahx-get
with the link defined in thehref
attribute - details which links hx-boost handles - then, same htmx attribute lookup procedure as above
- make all
- when
hx-inherit
is set on a parent node- when a parent node has a matching htmx attribute during the automatic lookup outlined above
hx-inherit="false"
disable all attribute inheritancehx-inherit="hx-select hx-get hx-target"
disable inheritance for only one or multiple specified attributes
<div hx-boost="true" hx-select="#content" hx-target="#content" hx-inherit="false">
<a href="/page1">Go To Page 1</a> <!-- boosted with the attribute settings above -->
<a href="/page2" hx-boost="unset">Go To Page 1</a> <!-- not boosted -->
<button hx-get="/test" hx-target="this"></button> <!-- hx-select is not inherited -->
</div>
<div hx-boost="true" hx-select="#content" hx-target="#content" hx-inherit="hx-target">
<!-- hx-select is automatically set to parent's value; hx-target is not inherited -->
<button hx-get="/test"></button>
</div>
<div hx-target="#content">
<div hx-boost="true" hx-select="#content" hx-inherit="hx-target">
<!-- hx-select is automatically set to parent's value -->
<!-- hx-target is inherited, because the direct parent does
not specify hx-target, thus cannot disable inheritance -->
<button hx-get="/test"></button>
</div>
</div>
Notes
hx-inherit="false"
orhx-inherit="hx-boost"
does not disablehx-boost
; instead usehx-boost="unset"
to disable inheritancehx-inherit="false"
orhx-inherit="<attribute"
only affects the inheritance outcome, if the element also has the htmx attribute defined that is currently being looked up- Find out more about Attribute Inheritance