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

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

Open
phillip-haydon opened this issue Jun 12, 2024 · 1 comment
Open

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

phillip-haydon opened this issue Jun 12, 2024 · 1 comment

Comments

@phillip-haydon
Copy link

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
Copy link
Collaborator

Hello,

  • You seem to be using closest while your form is not a child of the button. As you said:

the trigger button is outside the form

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

  • Also please note that, unfortunately, the extended CSS selectors are a bit limited at this time ; closest can only go up the hierarchy but cannot chain selectors for now. So here, closest .invite-button .htmx-indicator which should normally mean "find the closest .invite-button, then from there find the first .htmx-indicator child" will actually not work (that's something we'll want to fix in the future though!), while closest .invite-button would be a valid selector.

  • As for traversing back/forward, you might want to look into next and previous extended selectors that may suit your usecase here

Hope this helps!

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

No branches or pull requests

2 participants