htmx icon indicating copy to clipboard operation
htmx copied to clipboard

swapError with hx-on and hx-swap="innerHTML"

Open SamDudley opened this issue 2 years ago • 2 comments

I'm getting a swapError whilst trying to hx-swap="innerHTML" an element which has the new hx-on attribute.

I've put together this jsfiddle to demonstrate the issue (click the "Swap" button and look at the console).

I've also put together this jsfiddle which demonstrates a similar behavior working using hyperscript.

I've read through the docs for hx-on and don't see any limitations outlined which would prevent my example from working.

Edit 1:

It looks like it's trying to perform for...of loop over an object. image image

Edit 2:

Simply switching it to a for...in works and all the tests pass. image

Edit 3:

I'm going to add a failing test, then I'll test my code against that and open up a PR to be reviewed.

SamDudley avatar Apr 12 '23 13:04 SamDudley

I've opened a pull request.

I've made the for...of -> for...in change and added a test under hx-on which checks that hx-on works before and after being swapped using innerHTML.

I wasn't sure if to add a hasOwnProperty check as most of the code which uses for...in does, however in processHxOn the check is ommited.

Happy to discuss and update the PR as needed.

SamDudley avatar Apr 12 '23 14:04 SamDudley

I can confirm this issue. I update some element classes on htmx:beforeRequest and htmx:afterRequest with hx-on, resulting in the same error. The proposed fix works for me.

bezin avatar Apr 25 '23 08:04 bezin