-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hx-trigger~~'s consume modifier~~ has surprising behaviour with forms #2919
Comments
Yeah It is interesting. documentation points out in the first form you have there the button click event is consumed so it will not bubble up to the form and trigger the form submit as this is how buttons in forms trigger things. And because the button has no other hx-attributes htmx has no action to complete itself in this case so does nothing. In the second situation you have used the form=id attribute on the button which the browser uses to retarget the submit trigger from the button click to the form located somewhere else on the page. So you would expect the htmx consume here would prevent the button click bubbling up to its parent form (but there is none) in this case and its probably working as expected. But the default browser behavior of redirecting events to a remote form will still be in place as htmx doesn't alter default browser functions by design. |
If you read the documentation very literally, it only claims that hx-trigger's consume modifier affects other htmx requests, which should not include vanilla HTML form submission. So, regardless of what's the intended behaviour wrt forms here, the documentation doesn't describe the current behaviour accurately. |
Yeah after all it all comes down to this about Lines 2439 to 2441 in 326ff3b
The docs could probably be improved to specify that Though, it should be noted here that it's not
Using your example above, you can see on this JSFiddle that even without the The Lines 2396 to 2398 in b71af75
shouldCancel itself, will return true if the element is a submit button inside a form and the event is a click or a submitLines 2320 to 2322 in b71af75
The idea being that if a button has a So I would say this is a bug, we actually didn't have any support for the Line 2715 in b71af75
So, to sum it up:
If you'd like to look into it, feel free to investigate and submit a bugfix PR! And add test cases of course 😁 |
E: see below for details,
consume
is completely orthogonal to the issue (it's rather about hx-trigger's presence alone affecting the two forms differently)hx-trigger="click consume"
seems to prevent the button from triggering its form when its enclosed in that form, but not when it references its form by id. In the following example, "click me 1" does nothing while "click me 2" causes an attempt at form submission:The documentation doesn't explicitly say that such form submissions will be prevented (it only mentions other htmx requests; admittedly it calls out htmx requests on parents), but I'd expect that not to differ between these two setups, given that they're used interchangeably (and ttbomk do not differ in behaviour in HTML at all).
The text was updated successfully, but these errors were encountered: