Trigger hx-get from JS
See original GitHub issueI have a cart-count element that
<span id="cart-count" hx-get="/cart/count"
hx-trigger="load delay:1s" hx-target="#cart-count" hx-swap="outerHTML">
</span>
will 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 to remove from cart), send
response['HX-Trigger-After-Swap'] = "cart-updated"
to the frontend.
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?
Actually, it would be very convenient if I can write $cart-count as
hx-trigger="load delay:1s, cart-updated"
so that no JS is needed.
Update: According to https://github.com/bigskysoftware/htmx/issues/187 , hx-trigger="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 me so some manual way to trigger hx-get is still needed.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
get the element that triggers a javascript function
i want to know if i can access the element that triggers a function in javascript. i know... it's kinda hard to understand...
Read more >.trigger() | jQuery API Documentation
The .trigger() method can be used on jQuery collections that wrap plain JavaScript objects similar to a pub/sub mechanism; any event handlers bound...
Read more >How to trigger events in JavaScript ? - GeeksforGeeks
In order to work on JavaScript trigger events, it is important to know what is an event. An event is an interaction between...
Read more >jQuery trigger() Method - W3Schools
The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements.
Read more >How to Trigger Select Change Event in Javascript - YouTube
... you will learn how to trigger select change event in javascript.Source Code:https://www.fwait.com/how-to- trigger -select-change-event-...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Just to confirm that the solution works perfectly.
That is cool!
I was thinking along the line of
but this
from:bodysolution is good enough.