question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

hx-swap-oob requires target having an ID

See original GitHub issue

I have potentially multiple avatar in a page so the avatar is defined as

<div class="avatar">
</div>

without an id.

After the avatar is updated from a hx-post="update_profile" somewhere, the returned piece has

<div class="avatar" hx-swap-oob="true:.avatar">
</div>

which, according to the doc on hx-swap-oob, would update the first avatar, which would be the one on the header and I am ok with it. However, the code triggers

DOMException: Failed to execute 'querySelector' on 'Document': '#' is not a valid selector.

from what I believe by line 453 of this piece of code

https://github.com/bigskysoftware/htmx/blob/c26e3c10bbcee836ea0ba7b091525737ac6cfd00/src/htmx.js#L441-L454

despite the fact that the oobElement is found by class name so no id is required.

https://github.com/bigskysoftware/htmx/blob/c26e3c10bbcee836ea0ba7b091525737ac6cfd00/src/htmx.js#L470-L474

The code also appears to suggest that all matching elements will be replaced (which is even better for me), instead of “If a selector is given, the first element matching that selector will be swapped.”.

Is this a bug?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bencrokercommented, Apr 5, 2022

Best to create a new issue for this, this issue has become stale and too long.

0reactions
BoPengcommented, Apr 5, 2022

My PR was wrong but the feature request was valid.

Read more comments on GitHub >

github_iconTop Results From Across the Web

hx-swap-oob - </> htmx
The hx-swap-oob attribute allows you to specify that some content in a response should be swapped into the DOM somewhere other than the...
Read more >
Can hx-select-oob update several targets? - Stack Overflow
If your goal is to return html content and copy the same content to multiple targets - you simply use class selector instead...
Read more >
Out-of-band swaps with HTMX - .NET Guide - JetBrains
So, we have a cart button with an id of cartNavbar in our example. HTMX can process responses before placing them into our...
Read more >
Thymeleaf and htmx with out of band swaps - Wim Deblauwe
After that, it will use any additional HTML marked with hx-swap-oob="true" and swap that with the HTML already on the page, given the...
Read more >
HTMX and ASP.NET Core Swapping Techniques
Next, let's look at the fragment returned by ASP.NET Core. <div id="red" hx-swap-oob="true"> ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found