Learn hx-get in HTMX

hx-get="/order/12/status" sends a GET request when the element's natural trigger happens: click for buttons, divs and other elements, change for inputs…

You put hx-get on the Check status button and click it. The server's answer does arrive. But the "Check status" label on the button disappears too, replaced by the word "Ironing", and the status box next to it doesn't change at all. It's the most common thing to happen on your first day with htmx, and the cause is one attribute you haven't written yet.

hx-get="address" makes an element send a GET request to that address when its trigger happens. GET means asking for data, it changes nothing on the server. The HTML reply goes inside the element itself, unless hx-target points somewhere else. It's like asking Mrs. Rini at the laundry counter: "How far along is order #12?" If you don't point at the board, she writes the answer on the slip you hand her, your own question slip. If you point at order #12's status box on the board, the answer goes there.

When the request is sent: natural triggers

A trigger is the event that makes htmx send the request. An event is something that happens on the page, such as a click or a box's value changing. Every element has a natural trigger:

Element Natural trigger When
<input>, <select>, <textarea> change its value changes, then you press Enter or move to another box
<form> submit the form is submitted
any other element: <button>, <a>, <div>, ... click it is clicked

So <button hx-get="..."> runs on click, with no extra attribute.

Without hx-target, the reply goes into the element itself

<!-- before the click -->
<button hx-get="/order/12/status">Check status</button>

<!-- the server replies: Ironing -->

<!-- after the click -->
<button hx-get="/order/12/status">Ironing</button>

The "Check status" label is gone, replaced by "Ironing", inside the button itself. htmx replaces the innerHTML, the content between the opening and closing tags, of the element that fired the request. The button is still there, but its label is broken.

With hx-target, the reply goes where you point

This is order #12 on Mrs. Rini's Laundry board, now with a clear destination:

<li id="order-12">
  #12 Mr. Darto, 3 kg: <span id="status-12">Washing</span>
  <button id="check-12" hx-get="/order/12/status" hx-target="#status-12">
    Check status
  </button>
</li>

Loading itself when the page opens

Mrs. Rini's board has a summary box that has to fill in without anyone clicking:

<div id="summary" hx-get="/summary" hx-trigger="load" hx-target="this">
  Loading the summary...
</div>

What is sent to the server

Clicking Check status above sends this request:

GET /order/12/status
HX-Request: true
HX-Trigger: check-12
HX-Target: status-12
HX-Current-URL: http://localhost:8000/board

⚠️ Replies that land in the wrong place, or requests that never go

TL;DR: hx-get="/order/12/status" sends a GET request when the element's natural trigger happens: click for buttons, divs and other elements, change for inputs, selects and textareas, submit for forms. Without hx-target, the reply replaces the element's own content, so write hx-target="#status-12" to point somewhere else. hx-trigger="load" makes an element load itself when the page opens, and hx-target="this" points at the element itself. A GET carries values in the URL (/search?q=darto) and the header HX-Request: true. The server replies with a piece of HTML, not a whole page.

⚡ Without hx-target, the reply goes into the button itself

Write:

<button hx-get="/order/12/status">Check status</button>
Guess first: the server replies Ironing. Where does that reply go?

Inside the button itself. htmx replaces the innerHTML of the element that sent the request, so the Check status label is gone, replaced by Ironing.

Meaning: Buttons, divs and other elements send their request on a click. Inputs, selects and textareas send it on change, forms on submit. Without hx-target, the reply replaces the element's own content.

⚡ Point at the destination with hx-target

Write:

<li id="order-12">
  #12 Mr. Darto, 3 kg: <span id="status-12">Washing</span>
  <button id="check-12" hx-get="/order/12/status" hx-target="#status-12">
    Check status
  </button>
</li>

Meaning: The value of hx-target is a CSS selector, and # means id. The reply Ironing becomes the content of #status-12, and the button isn't touched. The target has to be on the page already. If it isn't found, htmx sends no request at all.

⚡ Loading itself when the page opens

Write:

<div id="summary" hx-get="/summary" hx-trigger="load" hx-target="this">
  Loading the summary...
</div>

Meaning: hx-trigger="load" sends the request once, as soon as the element is on the page. hx-target="this" points at the div itself. Don't write hx-trigger="": an empty value counts as missing, and the div goes back to waiting for a click.

What you will learn