Skip to content

Commit

Permalink
Update HTMX initialization and add MutationObserver for dynamic elements
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardolat committed Aug 4, 2024
1 parent 2d2f8fa commit 72ce05b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions internal/view/static/js/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { initNotyf } from './init-notyf.js'
import { initHTMXTriggers } from './init-htmx-triggers.js'
import { initHTMX } from './init-htmx.js'
import { initAlpineComponents } from './init-alpine-components.js'
import { initCopyFunction } from './init-copy-function.js'

initNotyf()
initHTMXTriggers()
initHTMX()
initAlpineComponents()
initCopyFunction()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function initHTMXTriggers () {
export function initHTMX () {
const triggers = {
ctm_alert: function (evt) {
const message = decodeURIComponent(evt.detail.value)
Expand Down Expand Up @@ -42,4 +42,19 @@ export function initHTMXTriggers () {
for (const key in triggers) {
document.addEventListener(key, triggers[key])
}

/*
This fixes this issue:
https://stackoverflow.com/questions/73658449/htmx-request-not-firing-when-hx-attributes-are-added-dynamically-from-javascrip
*/
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === 1 && !node['htmx-internal-data']) {
htmx.process(node)
}
})
})
})
observer.observe(document, { childList: true, subtree: true })
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"location",
"toaster",
"Alpine",
"Notyf"
"Notyf",
"htmx",
"MutationObserver"
]
}
}

0 comments on commit 72ce05b

Please sign in to comment.