Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 28 ยท HTMX Basics

Verbs, Triggers and Parameters

Use hx-get, hx-post, hx-put, hx-patch and hx-delete, control when requests fire with hx-trigger, and decide what is sent with hx-vals, hx-include and hx-params.

A first request is a single GET on a click. Real pages need more: create, update and delete data, react to typing, load something the moment the page opens, poll every few seconds, and send exactly the right values. HTMX covers all of this with three groups of attributes: verbs (which HTTP method), triggers (when to send) and parameters (what to send).

If you have not sent a request yet, start with your first HTMX request. The examples here use the same in-page mock server.

The five verbs

HTMX has one attribute for each HTTP method that browsers can send from JavaScript:

One attribute per method

hx-get Read something. Safe to repeat.
hx-post Create something new.
hx-put Replace a whole record.
hx-patch Change part of a record.

And hx-delete removes a record.

1 / 2

Plain HTML forms can only send GET and POST. HTMX lets any element send any of the five, so your URLs and methods can follow ordinary REST conventions. Use GET for requests that only read data, and the others for changes.

All five verbs

The mock server routes by both method and path, which is why the same URL can do different things. In a real application the router does the same.

Triggers

hx-trigger says which event starts the request. Every element has a sensible default:

Element Default trigger
<input>, <textarea>, <select> change
<form> submit
Everything else click

Set hx-trigger to use another event: hx-trigger="mouseenter", hx-trigger="keyup", or any DOM event name. You can list several, separated by commas.

Special triggers

HTMX adds a few triggers that are not DOM events:

  • load: fires when the element is loaded into the page.
  • revealed: fires once when the element scrolls into view (lazy loading, infinite scroll).
  • intersect: like revealed, with options such as threshold.
  • every 2s: polls repeatedly. Units are s and ms.

Trigger modifiers

Modifiers follow the event name, separated by spaces:

Modifier Effect
once Fire only the first time
changed Only if the element's value changed since the last request
delay:500ms Wait; restart the timer if the event happens again (debounce)
throttle:1s Ignore events for a period after one fires
from:<selector> Listen on another element, such as from:body or from:#search
target:<selector> Only if the event came from a matching element
consume Stop the event from bubbling further

A filter can also be written in square brackets: hx-trigger="keyup[key=='Enter']".

Triggers in action

Notice what is sent: the select and the input have a name, and a GET request puts that name and value in the query string (/size?size=small). The element's own value is included automatically when it triggers the request.

Polling with every keeps hitting your server for as long as the page is open. Keep intervals reasonable, and have the server stop the loop (by returning HTTP status 286 in HTMX) when there is nothing more to wait for.

What gets sent

HTMX decides which values to include using a few simple rules:

  1. For a GET or DELETE, parameters go in the query string. For POST, PUT and PATCH they go in the body, encoded as application/x-www-form-urlencoded, just like a classic form.
  2. If the element triggering the request has a name and a value (an input, select or textarea), that value is sent.
  3. For non-GET requests, the enclosing form's values are sent too.
  4. Anything else you add yourself with hx-vals, hx-include or hx-headers.

hx-vals

hx-vals adds extra values as JSON. It is handy for ids and flags that you do not want in a visible field.

hx-include

hx-include is a CSS selector for other elements whose values should be sent too: hx-include="#filters" or hx-include="[name='sort']". If you point at a form or a container, all inputs inside are included.

hx-params

hx-params filters which parameters go out: * (all, the default), none, name1,name2 (only those) or not secret (everything except). It is a convenience, not a security tool. The user can always edit the request.

hx-vals, hx-include and hx-params

Try each button and compare the tables. The first sends the form's fields; the second leaves out secret; the third sends only the two values from hx-vals; the last combines an outside input, the form and an extra value. A button inside a form would have included the form automatically, but these buttons sit outside it, so hx-include is required.

Inside the <form>, a request from a button with hx-post includes all the form's fields without hx-include. The forms lesson builds on exactly that.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is the default trigger for an <input> element?

    Inputs, textareas and selects fire on change, forms fire on submit, and other elements fire on click.
  2. 2. What does hx-trigger="keyup changed delay:400ms" do?

    delay waits for a pause (a debounce) and changed skips the request when the value is the same as the last time.
  3. 3. A request must include an input that sits outside the form. Which attribute helps?

    hx-include takes a selector of other elements whose values are sent. hx-vals adds literal JSON values, and hx-params only filters.
  4. 4. Where do the parameters of an hx-post request go?

    GET and DELETE use the query string; POST, PUT and PATCH send a form-encoded body.

Summary

  • hx-get, hx-post, hx-put, hx-patch and hx-delete choose the HTTP method.
  • hx-trigger chooses the event; defaults are change for fields, submit for forms and click for the rest.
  • Special triggers (load, revealed, every Ns) and modifiers (once, changed, delay, throttle, from) fine-tune when requests fire.
  • The triggering element's value, an enclosing form (non-GET), hx-include and hx-vals decide what is sent; hx-params filters it.
  • GET and DELETE send a query string; POST, PUT and PATCH send a form-encoded body.

Next, targets and swap strategies.