bigskysoftware / bigskysoftware/htmx
hx-target with a .class selector only swaps the first matching element
- Dominant language
- JavaScript
- Stars
- 49.4k
- Forks
- 1.7k
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 30
Description
### Scenario
Every post has a `.post-toolbar` so users can like the post. When a user clicks the post, it opens a dialog for detail with the same `.post-toolbar` component.
```html
```
### Expected
If the user likes the post in `#post-detail-123`, the `#post-123`’s toolbar should also reflect the updated state.
### Actual Problem
In HTMX 2.0, `hx-target=".post-toolbar-123"` only updates the first matching element in the DOM, so **only one** of the toolbars is swapped instead of both.
### Related
The [multiswap](https://v2-0v2-0.htmx.org/extensions/multi-swap/) extension doesn’t work because it’s designed for multiple IDs (e.g., `#id`, `#id2`), not classes.
> Only elements with an id selector are supported, as the function internally uses OOB internal method. So it is not possible to use class or any other selectors.
Similiar disscussion in [#44](https://github.com/bigskysoftware/htmx/issues/44#issuecomment-1272044822)
> If we use `hx-target` this will catch just the first.
> What if a `hx-targets` attribute where under the hood performs a querySelectAll?
Or a question on Stackoverflow: [How to update 2 target with htmx?](https://stackoverflow.com/questions/76451723/how-to-update-2-target-with-htmx)
Contributor guide
Assessment
This issue has not been assessed yet.