bigskysoftware / bigskysoftware/htmx

HTMX 2.0: Inline JS in swapped fragments no longer works reliably and CSP prevents hx-on

Open
#3,418 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
49.4k
Forks
1.7k
Avg merge
3d 22h
Merged PRs (30d)
30

Description

When I swap an HTMX fragment, I sometimes want to run JavaScript in that fragment immediately and only there. This is great for keeping the JavaScript local to the fragment and ensures it is removed when the fragment is destroyed or reloaded. Since HTMX 2.0, this no longer works . I have to use `htmx:load `or a similar event.
I cannot use `hx-on ` because of a very strict Content Security Policy (CSP).

How can I fix this without a hacky solution or excess use of global event listeners?

my.html:
```


Hello from fragment




```

my.js:
```
console.log("Fragment JS executed");
```

Thank you!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.