Adding a second trigger breaks change trigger #3575
Replies: 1 comment
-
|
The issue comes down to how HTMX registers event listeners under the hood. When you add a The fix is to use the <form hx-post="/endpoint"
hx-trigger="change from:input[type=checkbox], click[event.target.matches(.btn)]">
<input type="checkbox" name="item1" />
<input type="checkbox" name="item2" />
<button class="btn" type="button">Cancel</button>
</form>By scoping If you want to be even more explicit (and I usually prefer this for clarity), split the cancel button into its own HTMX element entirely: <form hx-post="/update" hx-trigger="change">
<input type="checkbox" name="item1" />
<input type="checkbox" name="item2" />
</form>
<button class="btn"
hx-post="/cancel"
hx-target="..."
hx-trigger="click">
Cancel
</button>Separating the triggers onto separate elements removes the ambiguity completely and makes each behavior easier to reason about individually. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a form with checkboxes that successfully triggers requests with
hx-trigger="change", but when I add a button to cancel selections, and try appending a trigger like:hx-trigger="change, click[event.target.matches('.btn')]"now the checkboxes are uncheckable and no requests are submitted.
Beta Was this translation helpful? Give feedback.
All reactions