bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
35.21k stars 1.17k forks source link

`closest` selector gets null on 'htmx-internal-data' #2613

Open phillip-haydon opened 3 weeks ago

phillip-haydon commented 3 weeks ago

I maybe confused about how 'closest' works but I have a form which is loaded in a modal, and sometimes the same form in the page.

In both cases the trigger button is outside the form because of it displaying in different positions in the page vs modal.

Everything works with the submission without issue, however trying to work with hx-indicator / hx-disabled-elt I have issues.

<div....
  <form ....
      hx-indicator="closest .invite-button .htmx-indicator"
      hx-disabled-elt="closest .invite-button">

  </form>
</div>

<button class="invite-button">
    <i class="loading htmx-indicator"></i>
    Invite
</button>

So when I run this I get:

TypeError: Cannot read properties of null (reading 'htmx-internal-data')

Shouldn't 'closest' be able to traverse back/forward from the trigger point (the form) to find the element with the class?

If I remove 'closest' it works but if the modal is open it makes the pages button also show the indicator and disabled state. (its not submitted, its just causing the button state to change even tho its not 'close' to the form being submitted)

Telroshan commented 3 weeks ago

Hello,

But closest, as per the docs,

will find the closest ancestor element or itself, that matches the given CSS selector

So if your form is not a child of that button you're trying to target, htmx won't ever be able to reach it as none of the form's ancestors matches the selector

Hope this helps!