Learn hx-swap in HTMX

hx-swap controls how the reply is put into the target. The default is innerHTML (the content is replaced), so a list is wiped if you forget to write hx-swap…

The "Load more" button under the order history has to add older orders below the ones already shown, not wipe them. The Delete button on one card has to throw away just that card. The search results box only needs its content replaced. Three buttons, three ways of placing a reply.

hx-swap decides how the HTML reply is put into the target: replacing its content, replacing the element itself, adding at the start or the end, inserting outside the element, or removing it. hx-target answers "where", hx-swap answers "how". The order board at Mrs. Rini's Laundry works the same way. Sometimes Mrs. Rini only changes the writing on one card, sometimes she takes a card down and pins up a new one, sometimes she pins a new order card at the bottom, and sometimes she takes down the card of laundry that was picked up. Same board, different ways.

Eight ways to place the reply

Their positions around the target <ul id="order-board">:

<!-- beforebegin: before the <ul> -->
<ul id="order-board">
  <!-- afterbegin: at the start of its content -->
  <li id="order-12">#12 Mr. Darto, 3 kg: <span>Washing</span></li>
  <li id="order-13">#13 Ms. Wulan, 2 kg: <span>Ironing</span></li>
  <li id="order-14">#14 Mr. Yoga, 5 kg: <span>Ready for pickup</span></li>
  <!-- beforeend: at the end of its content -->
</ul>
<!-- afterend: after the </ul> -->
hx-swap What happens to the target
innerHTML its content is replaced by the reply (the default, when hx-swap isn't written)
outerHTML the target element itself is replaced by the reply
afterbegin the reply is added at the start of its content, before the first item
beforeend the reply is added at the end of its content, after the last item
beforebegin the reply is inserted right before the target, outside it
afterend the reply is inserted right after the target, outside it
delete the target is removed, and the reply's content is ignored
none the request is still sent, but the page doesn't change

There's also textContent, which puts the reply in as plain text, so its tags show as they are.

innerHTML: the default, and why it surprises people

<form hx-post="/order" hx-target="#order-board">
  <input name="name" value="Mrs. Tini">
  <input name="weight" value="4">
  <button>Take in laundry</button>
</form>

Before reading on, take a guess. The <ul id="order-board"> board above holds 3 orders, this form sends a new order with no hx-swap, and the server replies with one <li>. After the form is sent, how many orders are on the board?

The server replies <li id="order-15">#15 Mrs. Tini, 4 kg: <span>Queued</span></li>. With no hx-swap, htmx uses innerHTML: the whole content of the <ul> is replaced by the reply. The board has only 1 order left, #15, and the three old orders are gone. innerHTML suits a box whose content really is replaced whole, such as a summary or search results.

beforeend: add at the end of the list

<form hx-post="/order" hx-target="#order-board" hx-swap="beforeend">

outerHTML: replace the element itself

<li id="order-12">#12 Mr. Darto, 3 kg: <span>Ironing</span>
  <button hx-post="/order/12/ready" hx-target="closest li" hx-swap="outerHTML">
    Mark ready
  </button>
</li>

delete and none

Modifiers: waiting and scrolling

After the way's name you can add modifiers, separated by spaces:

<button hx-delete="/order/14" hx-target="closest li" hx-swap="delete swap:1s">
  Picked up
</button>

<style>
  li.htmx-swapping { opacity: 0; transition: opacity 1s ease-out; }
</style>

⚠️ Lists that get wiped and ids that go missing

TL;DR: hx-swap controls how the reply is put into the target. The default is innerHTML (the content is replaced), so a list is wiped if you forget to write hx-swap. outerHTML replaces the element itself (its reply must carry the id again), beforeend and afterbegin add at the end or the start of the content, beforebegin and afterend outside the element, delete removes the target, none leaves the page alone. Modifiers such as swap:1s (the target wears htmx-swapping while it waits), settle:200ms and scroll:top come after the way's name. A misspelt value quietly becomes innerHTML.

⚡ The default is innerHTML

Write:

<form hx-post="/order" hx-target="#order-board">
  <input name="name" value="Mrs. Tini">
  <input name="weight" value="4">
  <button>Take in laundry</button>
</form>
Guess first: the board holds 3 orders, this form sends a new order with no hx-swap, and the server replies with one <li>. How many orders are on the board afterwards?

Only 1, #15. With no hx-swap, htmx uses innerHTML: the whole content of the <ul> is replaced by the reply, and the three old orders are gone.

Meaning: hx-target answers "where", hx-swap answers "how". innerHTML suits a box whose content really is replaced whole, such as search results.

⚡ beforeend: add at the end of the list

Write:

<form hx-post="/order" hx-target="#order-board" hx-swap="beforeend">

Meaning: Order #15 goes in after #14, and the three old orders stay. afterbegin is the opposite: the new one goes on top. A misspelt value, such as append, quietly becomes innerHTML.

⚡ outerHTML: replace the element itself

Write:

<li id="order-12">#12 Mr. Darto, 3 kg: <span>Ironing</span>
  <button hx-post="/order/12/ready" hx-target="closest li" hx-swap="outerHTML">
    Mark ready
  </button>
</li>

Meaning: The whole old <li>, its button included, is replaced by the new card from the server. The reply has to carry id="order-12" again. If it doesn't, the next request to #order-12 isn't sent.

What you will learn