high-article-21845
12/11/2022, 12:07 PMhigh-article-21845
12/11/2022, 12:08 PMhigh-article-21845
12/11/2022, 12:08 PMripe-action-67367
12/11/2022, 12:08 PMminiature-lizard-24702
12/11/2022, 12:08 PMhigh-article-21845
12/11/2022, 12:08 PMhigh-article-21845
12/11/2022, 12:08 PMminiature-lizard-24702
12/11/2022, 12:09 PMminiature-lizard-24702
12/11/2022, 12:10 PMhigh-article-21845
12/11/2022, 12:10 PMhigh-article-21845
12/11/2022, 12:10 PMhigh-article-21845
12/11/2022, 12:11 PMminiature-lizard-24702
12/11/2022, 12:11 PMminiature-lizard-24702
12/11/2022, 12:11 PMminiature-lizard-24702
12/11/2022, 12:12 PMhigh-article-21845
12/11/2022, 12:13 PMhigh-article-21845
12/11/2022, 12:13 PMhigh-article-21845
12/11/2022, 12:13 PMhigh-article-21845
12/11/2022, 12:13 PMminiature-lizard-24702
12/11/2022, 12:14 PMminiature-lizard-24702
12/11/2022, 12:16 PMminiature-lizard-24702
12/11/2022, 12:17 PMminiature-lizard-24702
12/11/2022, 12:18 PMminiature-lizard-24702
12/11/2022, 12:18 PMfuture-jewelry-30383
12/11/2022, 1:36 PMButton click --> opens modal --> plain xhr call fetches form page --> form html is rendered --> on form submit, htmx swaps the form showing either the form errors or a success message.
I'm finding myself in an annoying situation where I have to 1) Init my form components on DOMContentLoaded
when my website loads, 2) init my form components again when the modal content is loaded over xhr and 3) init my form components again on htmx:afterSwap
because it's dynamic DOM content in step 2 and 3 and it needs to initialise again. And if I have a form field somewhere outside of the modal, it risks getting 2+ event listeners attached to it because I re-run the form init on certain events.
I read about event delegation but a form element should init when it gets shown, while the examples of event delegation are usually click or change events on the document
or window
?
Any tips to make this a bit smoother?polite-glass-80210
12/11/2022, 2:28 PMrefined-waiter-90422
12/11/2022, 2:41 PMpolite-glass-80210
12/11/2022, 2:45 PMmysterious-toddler-20573
12/11/2022, 3:15 PMelt
, you should be able to initialize things properly w/o any duplication on both full page loads as well as partial updates.quaint-hair-61231
12/11/2022, 3:48 PMhtml
<div hx-ext="sse" sse-connect="/server-url">
<div sse-swap="event1"></div>
<div sse-swap="event2">
<div sse-swap="event3"></div>
</div>
</div>
I'm running into an issue where event1 and event2 are swapping OK but event 3 is not. In order to get event3 to swap properly I had to do this:
html
<div hx-ext="sse" sse-connect="/server-url">
<div sse-swap="event1"></div>
<div sse-swap="event2">
<div hx-ext="sse" sse-connect="/server-url" sse-swap="event3"></div>
</div>
</div>