Skip to content

Commit cfae71a

Browse files
committed
improve event doco
1 parent 7c075e4 commit cfae71a

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

www/src/content/docs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ Other modifiers you can use for triggers are:
280280
| `capture` | listen during the capture phase, from the top down, rather than the bubble phase |
281281
| `passive` | tell the browser that the handler will not call `preventDefault()`, so the browser can scroll without waiting for your code |
282282

283-
Note that a selector with whitespace in `from` or `target` needs parentheses, for example `from:(form input)`.
283+
Selectors containing spaces or commas in `from` or `target` must be wrapped in quotes: `from:'form input'`, `from:'.a, .b'`.
284284

285285
Multiple triggers can be specified by separating the triggers with a comma.
286286

www/src/content/docs/htmx-events-guide.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: "htmx Events Guide"
33
description: "Listen for htmx events to log, cancel, and modify request and swap behavior."
4+
keywords: ["events", "lifecycle", "event listener", "hx-on", "htmx.on", "request context", "ctx", "cancel request", "preventDefault", "CSRF", "add header", "modify request", "retarget", "reswap", "intercept", "hook", "scripting"]
45
---
56

67
htmx triggers events at every step of the request and swap lifecycle. These events are the main extension point
@@ -55,6 +56,50 @@ Inside an `hx-on:*` attribute every property of `detail` is already in scope, so
5556

5657
Element lifecycle events use `detail.elt` instead, which is the element htmx processed.
5758

59+
## The ctx Object
60+
61+
`ctx` is the request context object. It is created when a request is triggered and lives until the swap lifecycle
62+
ends. Properties marked **writable** can be changed in an event handler to alter htmx's behavior.
63+
64+
| Property | Type | Writable | Description |
65+
|----------|------|----------|-------------|
66+
| `sourceElement` | `Element` | | The element that triggered the request |
67+
| `sourceEvent` | `Event` | | The DOM event that triggered the request |
68+
| `target` | `Element` | **yes** | The element that will receive the swapped content. Mutate in `htmx:after:request`. Note: `HX-Retarget` is applied after this event and will overwrite your value |
69+
| `swap` | `string` | **yes** | The swap style (e.g. `"innerHTML"`). Mutate in `htmx:after:request`. Note: `HX-Reswap` is applied after this event and will overwrite your value |
70+
| `select` | `string\|null` | **yes** | CSS selector from `hx-select`. Mutate in `htmx:after:request`. Note: `HX-Reselect` is applied after this event and will overwrite your value |
71+
| `selectOOB` | `string\|null` | **yes** | Selector string from `hx-select-oob`. Mutate in `htmx:after:request` |
72+
| `push` | `string\|null` | **yes** | URL to push into history. Mutate in `htmx:after:request`. Note: `HX-Push-Url` is applied after this event and will overwrite your value |
73+
| `replace` | `string\|null` | **yes** | URL to replace in history. Mutate in `htmx:after:request`. Note: `HX-Replace-Url` is applied after this event and will overwrite your value |
74+
| `confirm` | `string\|null` | | Confirmation message from `hx-confirm`. Read before any events fire — not writable from an event |
75+
| `transition` | `boolean` | **yes** | Whether to use the View Transitions API. Writable up to and including `htmx:before:swap` |
76+
| `request` | `object` | | Sub-object with request details — see below |
77+
| `response` | `object` | | Added after `fetch()`. Has `raw` (the `Response`), `status` (number), `headers` (`Headers`) |
78+
| `text` | `string` | **yes** | Added after the response body is read. The raw HTML string. Mutate in `htmx:after:request` — by `htmx:before:swap` the fragment is already parsed |
79+
| `title` | `string` | **yes** | Page title extracted from the response fragment. Set during `swap()` — writable up to `htmx:after:swap` |
80+
| `hx` | `object` | | Parsed `HX-*` response headers. Keys are lowercased with hyphens removed: `hx.trigger`, `hx.retarget`, `hx.reswap`, etc. |
81+
82+
### ctx.request
83+
84+
`ctx.request` maps directly onto the [Fetch API `RequestInit`](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit).
85+
Changes made in `htmx:config:request` are applied before the request is sent.
86+
87+
| Property | Type | Description |
88+
|----------|------|-------------|
89+
| `action` | `string` | The request URL |
90+
| `method` | `string` | HTTP method (`"GET"`, `"POST"`, etc.) |
91+
| `headers` | `object` | Request headers. Add or change headers here |
92+
| `body` | `FormData\|URLSearchParams\|null` | Request body. A `FormData` at `htmx:config:request` (the right place to add values). Encoded to `URLSearchParams` or `null` for GET/DELETE before `fetch()` |
93+
| `validate` | `boolean` | Whether HTML5 form validation runs before the request |
94+
| `credentials` | `string` | Fetch credentials mode. Defaults to `"same-origin"` |
95+
| `signal` | `AbortSignal` | Abort signal. Fires when `htmx:abort` is triggered on the element |
96+
| `abort` | `function` | Call to abort the in-flight request |
97+
| `mode` | `string` | Fetch mode. Always reset to `htmx.config.mode` — cannot be overridden per-element |
98+
| `form` | `Element\|null` | The associated form element, if any |
99+
| `submitter` | `Element\|null` | The submit button that triggered the request, if any |
100+
| `anchor` | `string` | URL fragment (the part after `#`), if present in the action URL |
101+
| `timeout` | `number\|string` | Request timeout. Set via `hx-config="timeout:5s"` |
102+
58103
## Cancelling An Event
59104

60105
htmx events are cancelable. Call `preventDefault()` to stop htmx from continuing:
@@ -171,7 +216,7 @@ htmx.on('htmx:config:request', function (evt) {
171216
Both are on the context, so you can change them up to the point of the swap:
172217

173218
```javascript
174-
htmx.on('htmx:before:swap', function (evt) {
219+
htmx.on('htmx:after:request', function (evt) {
175220
if (evt.detail.ctx.response.status === 404) {
176221
evt.detail.ctx.target = document.querySelector('#errors');
177222
}

www/src/content/docs/whats-new-in-htmx-4.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ works. Request queuing is now controlled exclusively by [`hx-sync`](/reference/a
9292
<div hx-trigger="click" hx-get="/test" hx-sync="this:queue all">...</div>
9393
```
9494

95+
### `hx-trigger` `from:` and `target:` selector quoting
96+
97+
In htmx 2, selectors with spaces in `from:` used parentheses: `from:(form input)`. In htmx 4, HCON parses these
98+
modifier values, so selectors containing spaces or commas must be wrapped in single quotes:
99+
100+
```html
101+
<!-- htmx 2 -->
102+
<input hx-trigger="keyup from:(closest form)">
103+
104+
<!-- htmx 4 -->
105+
<input hx-trigger="keyup from:'closest form'">
106+
```
107+
108+
Comma-separated selectors also need quoting: `target:'.a, .b'`.
109+
95110
### 60-second timeout
96111

97112
htmx 2 had no timeout (`0`). htmx 4 sets [`defaultTimeout`](/reference/config/htmx-config-defaultTimeout) to `60000`.

www/src/content/reference/01-attributes/07-hx-trigger.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ Two special values: `self` (only the element itself, not bubbled children) and `
170170
<div hx-trigger="click from:outside" hx-get="...">...</div>
171171
```
172172

173-
Selectors containing spaces or commas can be wrapped in quotes: `from:'closest form'`, `from:'.a, .b'`.
173+
Selectors containing spaces or commas should be wrapped in quotes: `from:'closest form'`, `from:'.a, .b'`.
174174

175175
### `target:<selector>`
176176

0 commit comments

Comments
 (0)