Skip to content
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-include - allow multiple extended selectors #2645

Open
rdehuyss opened this issue Jun 18, 2024 · 3 comments
Open

hx-include - allow multiple extended selectors #2645

rdehuyss opened this issue Jun 18, 2024 · 3 comments

Comments

@rdehuyss
Copy link
Sponsor

It would be really handy if hx-include could use multiple selectors, just like hx-trigger.

Reasoning:

  • htmx by default submits everything in the closest <form> element. This confused me as the html spec says that forms may not be nested.
  • I tried using nested <form> elements (to only submit a part for a partial update) but it is not allowed according to the spec and chrome does not support it
  • so, in the end, I had to remove all forms and I use hx-include to only post the input fields nested inside a div. But in that case, I cannot include another input field that is not part of that div but global.
@rdehuyss
Copy link
Sponsor Author

Relates to #1754

@Telroshan
Copy link
Collaborator

htmx by default submits everything in the closest form element. This confused me as the html spec says that forms may not be nested.

Please note this is the case for non-GET requests but most importantly for "isolated inputs", as a form's closest form is that very form itself. As per the closest' doc:

Returns the closest ancestor Element or itself, which matches the selectors.

htmx/src/htmx.js

Lines 3416 to 3419 in f9b3f88

// for a non-GET include the closest form
if (verb !== 'get') {
processInputValue(processed, priorityFormData, errors, closest(elt, 'form'), validate)
}

The idea here is that if you make a non-GET hx- request on an input that is inside a form, it's very likely that you'll want the full context along, i.e. the parent form's data. If the request comes from a form already, this won't do anything as the closest form is that same form.
You should indeed not use nested form elements and we don't encourage this at all.

As for hx-include, you should already be able to use multiple selectors.
As per the doc:

A CSS query selector of the elements to include.
A standard CSS selector resolves to document.querySelectorAll and will include multiple elements, while the extended selectors such as find or next only return a single element at most to include

Which means any standard CSS selector matching multiple elements, will return those multiple elements. The exception here are the extended selectors that htmx provides (closest, find, next, previous) which for now are limited to a single selector. But using IDs you may totally reach your goal using hx-include="#container1 .something, #container2" for example.

The reason that hx-trigger has this "multiple triggers" section is the doc is because it supports a mini-language syntax with the from modifier for example, among other modifiers.

Hope this helps!

@rdehuyss
Copy link
Sponsor Author

@Telroshan, thank you so much for the swift feedback, detailed info and all the help here. You are a 🦸 (hero 😊).

I'm aware of the fact that this is for all but hx-get AJAX requests and happy to read that nested forms are not encouraged. I think my confusion comes from the docs that mention the closest form which I interpreted as you can nest them.

I was trying the following and as it uses the extended selector, this indeed does not work:
hx-include="closest div.columns, #container2"

I shall rename the issue to reflect this feature request.

@rdehuyss rdehuyss changed the title hx-include - allow multiple selectors hx-include - allow multiple extended selectors Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants