| 1 | --- |
| 2 | title: "hx-trigger" |
| 3 | description: "Controls when the element issues a request" |
| 4 | --- |
| 5 | |
| 6 | The `hx-trigger` attribute controls which event(s) trigger an element's AJAX request (set via [`hx-get`](/reference/attributes/hx-get), [`hx-post`](/reference/attributes/hx-post), etc.). |
| 7 | |
| 8 | Defaults to: |
| 9 | - [`change`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event) → `<input>` / `<textarea>` / `<select>` |
| 10 | - [`submit`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event) → `<form>` |
| 11 | - [`click`](https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event) → `<input type=button>`, `<input type=submit>`, and everything else |
| 12 | |
| 13 | ## Syntax |
| 14 | |
| 15 | ```html |
| 16 | <!-- hx-trigger="<event>[<filter>] <modifiers> [, ...]" --> |
| 17 | |
| 18 | <!-- Basic (click is the default) --> |
| 19 | <button hx-get="..."> |
| 20 | |
| 21 | <!-- With from: modifier, listen on a different element --> |
| 22 | <button hx-trigger="click from:outside" hx-get="..."> |
| 23 | |
| 24 | <!-- With a filter --> |
| 25 | <button hx-trigger="click[shiftKey]" hx-get="..."> |
| 26 | |
| 27 | <!-- Multiple triggers --> |
| 28 | <button hx-trigger="click, keyup[key=='Enter']" hx-get="..."> |
| 29 | ``` |
| 30 | |
| 31 | ## Standard Events |
| 32 | |
| 33 | `hx-trigger` accepts any DOM event: `click`, `input`, `keyup`, `submit`, etc. |
| 34 | |
| 35 | ```html |
| 36 | <button hx-trigger="click" hx-post="..."> |
| 37 | <input hx-trigger="input" hx-get="..."> |
| 38 | <form hx-trigger="submit" hx-post="..."> |
| 39 | <div hx-trigger="mouseenter" hx-get="..."> |
| 40 | ``` |
| 41 | |
| 42 | ## Custom Events |
| 43 | |
| 44 | Custom events work too. Dispatch them from JavaScript with [`htmx.trigger()`](/reference/methods/htmx-trigger), or from the server via the [`HX-Trigger`](/reference/headers/HX-Trigger) response header. |
| 45 | |
| 46 | Events from `HX-Trigger` are dispatched on the `body`, so use [`from:body`](#from) to listen for them: |
| 47 | |
| 48 | ```html |
| 49 | <div hx-trigger="productsUpdated from:body" hx-get="...">...</div> |
| 50 | ``` |
| 51 | |
| 52 | ## Synthetic Events |
| 53 | |
| 54 | htmx provides synthetic events beyond standard DOM events: |
| 55 | |
| 56 | ### `load` |
| 57 | |
| 58 | Fires when the element is loaded into the DOM. Useful for [lazy-loading](/patterns/loading/lazy-load) content. |
| 59 | |
| 60 | ```html |
| 61 | <div hx-trigger="load" hx-get="...">Loading...</div> |
| 62 | ``` |
| 63 | |
| 64 | ### `revealed` |
| 65 | |
| 66 | Fires when the element is scrolled into the viewport. Useful for [infinite scroll](/patterns/loading/infinite-scroll). |
| 67 | |
| 68 | ```html |
| 69 | <div hx-trigger="revealed" hx-get="...">Loading...</div> |
| 70 | ``` |
| 71 | |
| 72 | _Note: `revealed` always observes the browser viewport. For scrollable containers with `overflow`, use [`intersect`](#intersect) with `root` instead._ |
| 73 | |
| 74 | ### `intersect` |
| 75 | |
| 76 | Fires when an element becomes visible in the viewport. |
| 77 | |
| 78 | Uses the [IntersectionObserver API](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#options) and supports [`root`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/root), [`rootMargin`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin), and [`threshold`](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/thresholds) as modifiers. |
| 79 | |
| 80 | ```html |
| 81 | <div hx-trigger="intersect once" hx-get="...">...</div> |
| 82 | <div hx-trigger="intersect root:#scroll-container" hx-get="...">...</div> |
| 83 | <div hx-trigger="intersect rootMargin:100px" hx-get="...">...</div> |
| 84 | <div hx-trigger="intersect threshold:0.5" hx-get="...">...</div> |
| 85 | ``` |
| 86 | |
| 87 | ### `every <time>` |
| 88 | |
| 89 | Fires repeatedly on an interval. |
| 90 | |
| 91 | ```html |
| 92 | <div hx-trigger="every 1s" hx-get="/updates">...</div> |
| 93 | ``` |
| 94 | |
| 95 | To add a filter to polling, add it after the interval: |
| 96 | |
| 97 | ```html |
| 98 | <div hx-trigger="every 1s [someConditional]" hx-get="/updates">...</div> |
| 99 | ``` |
| 100 | |
| 101 | ## Event Modifiers |
| 102 | |
| 103 | ### `[filter]` |
| 104 | |
| 105 | A JavaScript expression in brackets after the event name. Only fires when it evaluates to `true`. |
| 106 | |
| 107 | ```html |
| 108 | <input hx-trigger="keyup[key == 'Enter']" hx-get="/search"> |
| 109 | ``` |
| 110 | |
| 111 | Inside the brackets, all properties of the event are available as bare names: |
| 112 | |
| 113 | - [`click`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent#instance_properties) → `altKey`, `ctrlKey`, `shiftKey`, `metaKey`, ... |
| 114 | - [`keydown`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#instance_properties) → `key`, `code`, `repeat`, ... |
| 115 | |
| 116 | Global functions work too: `click[hasUnsavedChanges()]`. |
| 117 | |
| 118 | ### `once` |
| 119 | |
| 120 | Fires once, then stops listening. |
| 121 | |
| 122 | ```html |
| 123 | <button hx-trigger="click once" hx-get="...">Load Once</button> |
| 124 | ``` |
| 125 | |
| 126 | ### `changed` |
| 127 | |
| 128 | Only fires if the element's `value` changed since last time. |
| 129 | |
| 130 | ```html |
| 131 | <input hx-trigger="input changed" hx-get="..."> |
| 132 | ``` |
| 133 | |
| 134 | Note: [`change`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event) is a DOM event. `changed` is an htmx modifier. Different things. |
| 135 | |
| 136 | ### `delay:<time>` |
| 137 | |
| 138 | Waits before firing. If the event fires again, the delay resets (debounce). |
| 139 | |
| 140 | ```html |
| 141 | <input hx-trigger="input delay:1s" hx-get="..."> |
| 142 | ``` |
| 143 | |
| 144 | ### `throttle:<time>` |
| 145 | |
| 146 | Fires, then ignores further events for the given interval. |
| 147 | |
| 148 | ```html |
| 149 | <div hx-trigger="scroll throttle:500ms" hx-get="...">...</div> |
| 150 | ``` |
| 151 | |
| 152 | ### `from:<selector>` |
| 153 | |
| 154 | Listens on a different element. Takes a CSS selector or an [extended selector](/docs#extended-selectors-1). Two special values: `self` (only the element itself, not children) and `outside` (anything outside the element). |
| 155 | |
| 156 | ```html |
| 157 | <div hx-trigger="keyup[key=='Enter'] from:body" hx-get="...">...</div> |
| 158 | <div hx-trigger="my-event from:document" hx-get="...">...</div> |
| 159 | <div hx-trigger="submit from:closest form" hx-get="...">...</div> |
| 160 | <div hx-trigger="click from:self" hx-get="...">...</div> |
| 161 | <div hx-trigger="click from:outside" hx-get="...">...</div> |
| 162 | ``` |
| 163 | |
| 164 | ### `target:<selector>` |
| 165 | |
| 166 | Only fires if `event.target` matches the given CSS selector. |
| 167 | |
| 168 | ```html |
| 169 | <div hx-trigger="click target:.child-button" hx-get="...">...</div> |
| 170 | ``` |
| 171 | |
| 172 | ### `prevent` |
| 173 | |
| 174 | Calls `event.preventDefault()`. |
| 175 | |
| 176 | ```html |
| 177 | <form hx-trigger="submit prevent" hx-post="...">...</form> |
| 178 | ``` |
| 179 | |
| 180 | ### `stop` / `consume` |
| 181 | |
| 182 | Calls `event.stopPropagation()`. |
| 183 | |
| 184 | ```html |
| 185 | <button hx-trigger="click stop" hx-get="...">...</button> |
| 186 | ``` |
| 187 | |
| 188 | ### `halt` |
| 189 | |
| 190 | Shorthand for `prevent stop`. |
| 191 | |
| 192 | ```html |
| 193 | <a hx-trigger="click halt" hx-get="...">...</a> |
| 194 | ``` |
| 195 | |
| 196 | ### `capture` |
| 197 | |
| 198 | Listens during the capture phase (top-down) instead of the bubble phase (bottom-up). |
| 199 | |
| 200 | ```html |
| 201 | <div hx-trigger="click capture" hx-get="...">...</div> |
| 202 | ``` |
| 203 | |
| 204 | ### `passive` |
| 205 | |
| 206 | Tells the browser the handler won't call `preventDefault()`, so the browser can scroll without waiting for your code to finish. |
| 207 | |
| 208 | ```html |
| 209 | <div hx-trigger="scroll passive" hx-get="...">...</div> |
| 210 | ``` |
| 211 | |
| 212 | ### Example |
| 213 | |
| 214 | A search box that searches on `input`, but only if the value has [`changed`](#changed) and the user hasn't typed anything new for 1 second ([`delay`](#delay)): |
| 215 | |
| 216 | ```html |
| 217 | <input name="q" |
| 218 | hx-trigger="input changed delay:1s" |
| 219 | hx-get="/search" |
| 220 | hx-target="#search-results"/> |
| 221 | ``` |
| 222 | |
| 223 | ## Notes |
| 224 | |
| 225 | * Selectors with whitespace in [`from`](#from) or [`target`](#target) need parentheses: `from:(form input)`. |
| 226 | * `hx-trigger="change, reset"` may fire before the browser resets the form. As a workaround, add a short delay: `hx-trigger="change, reset delay:0.01s"`. |
| 227 | |
| 228 | ## See Also |
| 229 | |
| 230 | - [`hx-on`](/reference/attributes/hx-on) (attribute) |
| 231 | - [Extended Selectors](/docs#extended-selectors-1) (reference) |
| 232 | - [Lazy Load](/patterns/loading/lazy-load) (pattern) |
| 233 | - [Infinite Scroll](/patterns/loading/infinite-scroll) (pattern) |
| 234 | - [Progress Bar](/patterns/loading/progress-bar) (pattern) |