question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trigger hx-get from JS

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
BoPengcommented, Jan 25, 2021

Just to confirm that the solution works perfectly.

0reactions
BoPengcommented, Jan 24, 2021

That is cool!

I was thinking along the line of

response['HX-Trigger-Targets'] = '#cart-count`

but this from:body solution is good enough.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found