--- name: htmx-guidance description: Use when writing HTML with htmx, building htmx-powered pages, or answering questions about htmx patterns and best practices. Covers htmx 4 attributes, events, swap strategies, and common UI patterns. --- # htmx 4 Guidance htmx allows any HTML element to issue HTTP requests and swap the response into the DOM. The server returns **HTML fragments**, not JSON. This is the fundamental model. htmx 4 uses the `fetch()` API (not XMLHttpRequest like htmx 2). ## Core Attributes Issue requests with these attributes. Each takes a URL: | Attribute | Description | |-------------|----------------| | `hx-get` | GET request | | `hx-post` | POST request | | `hx-put` | PUT request | | `hx-patch` | PATCH request | | `hx-delete` | DELETE request | | `hx-query` | QUERY request | ### Default Triggers - `input`, `textarea`, `select` trigger on `change` - `form` triggers on `submit` - Everything else triggers on `click` Override with `hx-trigger`. ## hx-trigger Specify what event triggers the request: ```html
Hover me
``` **Modifiers:** | Modifier | Description | |---------------------|--------------------------------------------------------------------------------------| | `once` | fire only once | | `changed` | only fire if the value of the element changed | | `delay: