1.8 KiB
1.8 KiB
HTMX
Attributes
hx-[delete|get|patch|post|put]- AJAX method to usehx-swapinnerHTML- Default, replace hx-target inner html.outerHTML- Replace the hx-target itselfbeforebegin- Insert before hx-targetafterbegin- Insert before hx-target first childbeforeend- Insert after hx-target last childafterend- Insert response after hx-targetdelete- Deletes hx-target regardless of the response.none- No swap.
hx-target: CSS selector forhx-swapactionhx-trigger: comma-separated list of events triggering a request, often the default:- On
input,textarea,select:change - On
form:submit - On all other elements:
click - Filters:
- in
[]after the event name - Example:
keyup[ctrlKey && key == 'l']to catch CTRL-L
- in
- Specify event source with
from:<extended CSS selector>,- defaults to current element
- Example:
from:bodyto receive events from the whole body
- Complete spec: https://htmx.org/attributes/hx-trigger/
- On
hx-include:- when
hx-post-ing outside a form, specify which elements to include, as if they had been in a form along with the posting button - supports matching on multiple comma-separated selectors
- when
hx-vals: value as a JSON string or JS dynamic value- example:
hx-vals='{"state":"MT"}' - example:
hx-vals='js:{"state":getCurrentState()}'
- example:
Extended CSS selectors
- support multiple comma-separated selectors, but...
hx-target: return first matching element from first selector (?)hx-trigger from:andhx-include: return first matching child
- relative selectors:
closest:closest parent matching selector(previous|next):: previous or next element (not necessarily sibling) matching selectorfind:next child matching selectorthis:the current element