Event Methods
The following table summarizes the htmx methods related to events. These methods simplify adding event listeners to DOM elements, removing them, and triggering events on DOM elements.
Method | Description |
---|---|
htmx.off | removes an event listener from an element |
htmx.on | adds an event listener to an element |
htmx.trigger | triggers an event on an element |
All of these methods have a DOM equivalent.
For example, the following sets of statements are equivalent. Assume the variable sel holds a CSS selector string, the variable el holds a DOM element, the variable ev holds an event name, the variable cb holds a callback function that’s invoked when the event occurs, and the variable detail holds an arbitrary object that event listeners can use.
Get Server-Driven Web Apps with htmx now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.