I am using htmx to add some AJAX calls to my page. I have a cart-count
element that is defined to retrieve the number of items in the cart 1s after the page is loaded:
<span id="cart-count" hx-get="/cart/count"
hx-trigger="load delay:1s" hx-target="#cart-count" hx-swap="outerHTML">
</span>
I also have an add-to-cart
button that, after its own stuff (e.g. change itself to remove from cart
using htmx), send an extra attribute
response['HX-Trigger-After-Swap'] = "cart-updated"
to the frontend (see here for the doc).
I have an event listener that works,
document.body.addEventListener("cart-updated",
function (evt) {
alert("cart updated")
})
but how can I trigger hx-get
of the #cart-count
element in JS?
Note that
hx-trigger="load delay:1s, cart-updated"
would work if the cart stuff is in the parent chain of add-to-cart
element, but unfortunately this is not the case for my webpage.
Note that the question is also asked here