Skip to content

Problem with x-for and HTMX #4476

Answered by SimoTod
florian-sun asked this question in 1. Help
Dec 12, 2024 · 3 comments · 6 replies
Discussion options

You must be logged in to vote

This is a simple snippet of code to fix x-for and x-if when used with the HTMX history

    ...
    <script src = "https://unpkg.com/[email protected]" integrity="sha384-0895/pl2MU10Hqc6jd4RvrthNlDiE9U1tWmX7WRESftEDRosgxNsQG/Ze9YMRzHq" crossorigin="anonymous"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>
    <script>
        document.addEventListener('htmx:beforeHistorySave', (evt) => {
            document.querySelectorAll('[x-for]').forEach((item) => {
                item._x_lookup && Object.values(item._x_lookup).forEach((el) => el.remove())
            })
            document.querySelectorAll('[x-if]').forEach((item) => {

Replies: 3 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@SimoTod
Comment options

SimoTod Dec 12, 2024
Collaborator

Comment options

You must be logged in to vote
5 replies
@SimoTod
Comment options

SimoTod Dec 13, 2024
Collaborator

@SimoTod
Comment options

SimoTod Dec 13, 2024
Collaborator

@SimoTod
Comment options

SimoTod Dec 13, 2024
Collaborator

@SimoTod
Comment options

SimoTod Dec 13, 2024
Collaborator

Answer selected by florian-sun
@florian-sun
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants