Skip to content

An afterSwap event is not fired on swapped content #3190

Answered by Telroshan
lonix1 asked this question in Q&A
Discussion options

You must be logged in to vote

Hey,

Just as you suspected,

I assume that's because it removes the original content

Htmx indeed cleans up elements when replacing them (i.e. removes any htmx listener and internal data)

htmx/src/htmx.js

Lines 1720 to 1722 in 90a91a6

cleanUpElement(target)
if (target instanceof Element) {
target.remove()

  • Depending on what you want to do exactly in this case, you might rely on the htmx:beforeCleanupElement event that htmx fires right before unitializing an element (if you need to run some logic when it gets replaced, you could do it here)
  • Or, you could make use of hx-preserve
  • Other than that, you might indeed want to redesign your markup so your triggering ele…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@lonix1
Comment options

@lonix1
Comment options

Answer selected by lonix1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants