hx-swap
Thehx-swap
attribute allows you to specify how the response will be swapped in relative to thetarget of an AJAX request. If you do not specify the option, the default ishtmx.config.defaultSwapStyle
(innerHTML
).
The possible values of this attribute are:
innerHTML
- Replace the inner html of the target elementouterHTML
- Replace the entire target element with the responsetextContent
- Replace the text content of the target element, without parsing the response as HTMLbeforebegin
- Insert the response before the target elementafterbegin
- Insert the response before the first child of the target elementbeforeend
- Insert the response after the last child of the target elementafterend
- Insert the response after the target elementdelete
- Deletes the target element regardless of the responsenone
- Does not append content from response (out of band items will still be processed).These options are based on standard DOM naming and theElement.insertAdjacentHTML
specification.
So in this code:
<divhx-get="/example"hx-swap="afterend">Get Some HTML & Append It</div>
Thediv
will issue a request to/example
and append the returned content after thediv
Thehx-swap
attributes supports modifiers for changing the behavior of the swap. They are outlined below.
transition
If you want to use the newView Transitions APIwhen a swap occurs, you can use thetransition:true
option for your swap. You can also enable this feature globally bysetting thehtmx.config.globalViewTransitions
config setting totrue
.
swap
&settle
You can modify the amount of time that htmx will wait after receiving a response to swap the contentby including aswap
modifier:
<!-- this will wait 1s before doing the swap after it is received --> <divhx-get="/example"hx-swap="innerHTML swap:1s">Get Some HTML & Append It</div>
Similarly, you can modify the time between the swap and the settle logic by including asettle
modifier:
<!-- this will wait 1s before doing the settle after it is received --> <divhx-get="/example"hx-swap="innerHTML settle:1s">Get Some HTML & Append It</div>
These attributes can be used to synchronize htmx with the timing of CSS transition effects.
ignoreTitle
By default, htmx will update the title of the page if it finds a<title>
tag in the response content. You can turnoff this behavior by setting theignoreTitle
option to true.
scroll
&show
You can also change the scrolling behavior of the target element by using thescroll
andshow
modifiers, bothof which take the valuestop
andbottom
:
<!-- this fixed-height div will scroll to the bottom of the div after content is appended --> <divstyle="height:200px; overflow: scroll"hx-get="/example"hx-swap="beforeend scroll:bottom"> Get Some HTML & Append It & Scroll To Bottom </div>
<!-- this will get some content and add it to #another-div, then ensure that the top of #another-div is visible in the viewport --> <divhx-get="/example"hx-swap="innerHTML show:top"hx-target="#another-div"> Get Some Content </div>
If you wish to target a different element for scrolling or showing, you may place a CSS selector after thescroll:
orshow:
, followed by:top
or:bottom
:
<!-- this will get some content and swap it into the current div, then ensure that the top of #another-div is visible in the viewport --> <divhx-get="/example"hx-swap="innerHTML show:#another-div:top"> Get Some Content </div>
You may also usewindow:top
andwindow:bottom
to scroll to the top and bottom of the current window.
<!-- this will get some content and swap it into the current div, then ensure that the viewport is scrolled to the very top --> <divhx-get="/example"hx-swap="innerHTML show:window:top"> Get Some Content </div>
For boosted links and forms the default behaviour isshow:top
. You can disable it globally withhtmx.config.scrollIntoViewOnBoost or you can usehx-swap="show:none"
on an element basis.
<formaction="/example"hx-swap="show:none"> ...</form>
htmx preserves focus between requests for inputs that have a defined id attribute. By default htmx prevents auto-scrolling to focused inputs between requests which can be unwanted behavior on longer requests when the user has already scrolled away. To enable focus scroll you can usefocus-scroll:true
.
<inputid="name"hx-get="/validation"hx-swap="outerHTML focus-scroll:true"/>
Alternatively, if you want the page to automatically scroll to the focused element after each request you can change the htmx global configuration valuehtmx.config.defaultFocusScroll
to true. Then disable it for specific requests usingfocus-scroll:false
.
<inputid="name"hx-get="/validation"hx-swap="outerHTML focus-scroll:false"/>
hx-swap
is inherited and can be placed on a parent elementinnerHTML
outerHTML
method on the<body>
element.htmx will changeouterHTML
on<body>
to useinnerHTML
.