Debian Codegraph @

www/src/content/reference/05-methods/05-htmx-on.md

raw symbols SCIP
1 ---
2 title: "htmx.on()"
3 description: "Listen for htmx events"
4 ---
5
6 The `htmx.on()` function registers an event listener for htmx events.
7
8 ## Syntax
9
10 ```javascript
11 // Listen on document
12 htmx.on(event, callback)
13
14 // Listen on specific element
15 htmx.on(element, event, callback)
16 ```
17
18 ## Parameters
19
20 - `event` - Event name to listen for
21 - `callback` - Function to call when event fires
22 - `element` - Optional element to listen on (defaults to document)
23
24 ## Example
25
26 ```javascript
27 // Listen for all htmx requests
28 htmx.on('htmx:before:request', (evt) => {
29 console.log('Request starting:', evt.detail);
30 });
31
32 // Listen on specific element
33 htmx.on('#myForm', 'htmx:after:swap', (evt) => {
34 console.log('Form updated');
35 });
36 ```
37
38 ## Return Value
39
40 Returns the callback function, which can be used to remove the listener later.