Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using hx-swap="delete swap:1s", element is possibly deleted #2932

Closed
jackielii opened this issue Sep 25, 2024 · 0 comments · Fixed by #2933
Closed

Using hx-swap="delete swap:1s", element is possibly deleted #2932

jackielii opened this issue Sep 25, 2024 · 0 comments · Fixed by #2933

Comments

@jackielii
Copy link
Contributor

When using a hx-swap="delete swap:1s", the target element could have been deleted already.

To reproduce simply do:

<div id="add-users-modal" hx-replace>
  <button
		  hx-post="/add"
		  hx-target="#add-users-modal"
		  hx-swap="delete swap:1s"
		  @htmx:after-request="show=false"
		  type="button"
  >add<button>
</div>
<script>
document.addEventListener("htmx:beforeSwap", (event) => {
  const content = event.detail.serverResponse;
  if (content.includes("hx-replace")) {
    const parser = new DOMParser();
    const doc = parser.parseFromString(content, "text/html");
    const id = doc.querySelector("[hx-replace]").id;
    const el = event.detail.target.querySelector(`#${id}`);
    if (el) {
      el.remove();
    }
  }
});
<script>

Here hx-replace is an attribute I defined myself to replace if present else append.

In such use cases, I'll get htmx.esm.js:1699 Uncaught TypeError: Cannot read properties of null (reading 'removeChild')

image

jackielii added a commit to jackielii/htmx that referenced this issue Sep 25, 2024
jackielii added a commit to jackielii/htmx that referenced this issue Sep 26, 2024
jackielii added a commit to jackielii/htmx that referenced this issue Sep 26, 2024
1cg pushed a commit that referenced this issue Oct 3, 2024
* fix #2932: check parent is null for swap

* fix test in swap when parent elt deleted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant