-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Trigger hx-get from JS #339
Comments
Would you mind asking this question on stack overflow and linking to it, so we can build up some answers over there? That's where people tend to go for things like this, and I have a good answer for you. :) |
Like this? |
Perfect, I answered it there too, but here is what I said: You can listen for the event on the body of the document in the latest version of htmx using the <span hx-get="/cart/count"
hx-trigger="load delay:1s, cart-updated from:body"
hx-swap="outerHTML">
</span> Note also that you don't need to use an explicit target here since the default target is the element that the |
That is cool! I was thinking along the line of
but this |
Just to confirm that the solution works perfectly. |
I have a
cart-count
element thatwill try to
hx-get
the number of items in cart when the page is loaded.Now, I have an
add-to-cart
button that, after its own stuff (e.g. change itself toremove from cart
), sendto the frontend.
I have an event listener that works,
but how can I trigger
hx-get
of the#cart-count
element in JS?Actually, it would be very convenient if I can write
$cart-count
asso that no JS is needed.
Update: According to #187 ,
hx-trigger="cart-updated"
would work if the cart stuff is in the parent chain ofadd-to-cart
element, but unfortunately this is not the case for me so some manual way to triggerhx-get
is still needed.The text was updated successfully, but these errors were encountered: