htmx.on()
Listens for htmx events
The htmx.on() function registers an event listener for htmx events.
Syntax
// Listen on document htmx.on(event, callback) // Listen on specific element htmx.on(element, event, callback)
Parameters
-
event- Event name to listen for -
callback- Function to call when event fires -
element- Optional element to listen on (defaults to document)
Example
// Listen for all htmx requests
htmx.on('htmx:before:request', (evt) => {
console.log('Request starting:', evt.detail);
});
// Listen on specific element
htmx.on('#myForm', 'htmx:after:swap', (evt) => {
console.log('Form updated');
}); Return Value
Returns the callback function, which can be used to remove the listener later.
Licensed under the Zero-Clause BSD License.
https://four.htmx.org/reference/methods/htmx-on/