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

Add FAQ or explanaition on how HX-Trigger events bubble #187

Closed
thinkofher opened this issue Sep 6, 2020 · 3 comments
Closed

Add FAQ or explanaition on how HX-Trigger events bubble #187

thinkofher opened this issue Sep 6, 2020 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@thinkofher
Copy link

Hello everyone!

First off, I want to say that I really appreciate your work and found the htmx API very attractive.

Now we can move to my little issue. Let's consider following code.

<button hx-post="/event" hx-swap="innerHTML">
  Click Me
</button>
<div hx-get="/waiting" hx-trigger="myEvent" hx-swap="innerHTML">
  Waiting for myEvent.
</div>
<script>
  document.body.addEventListener("myEvent", function(evt){
    alert("myEvent was triggered!");
  })
</script>

HX-Trigger header of /event endpoint is set to myEvent. Can someone explain me why alert is firing but div with hx-trigger set to myEvent isn't?

@1cg
Copy link
Contributor

1cg commented Sep 7, 2020

The myEvent event is being triggered on the button and then bubbling up to the body which is why it is working for the script but not the element: the element is a sibling, not a parent. So, just like with a click event or any other event that bubbles, it will end up making it's way up to the body, but won't be triggered on siblings.

If you want to catch the event you need to be in the parent hierarchy of the receiving element:

<div hx-trigger="myEvent" hx-target="otherDiv"  hx-get="/waiting">
  <button hx-post="/event">
    Click Me
  </button>
</div>
<div id="otherDiv">
  Waiting for myEvent.
</div>
<script>
  document.body.addEventListener("myEvent", function(evt){
    alert("myEvent was triggered!");
  })
</script>

Here we've moved all the htmx attributes to a parent div. (I also eliminated the innerHTML since that's the default.) We are then using hx-target to target the sibling div we want replaced.

Hope that clarifies.

@1cg 1cg closed this as completed Sep 7, 2020
@clarkevans
Copy link

This seems like it's a FAQ like item; or part of a usage guide. There's no way that I'd know this nugget of information; the failed and the working example are very helpful.

@1cg 1cg reopened this Sep 8, 2020
@1cg 1cg changed the title How to trigger custom event? Add FAQ or explaination on how HX-Trigger events bubble Sep 8, 2020
@1cg
Copy link
Contributor

1cg commented Sep 8, 2020

Reopened for a doc update

@bencroker bencroker added the documentation Improvements or additions to documentation label Sep 16, 2020
@bencroker bencroker changed the title Add FAQ or explaination on how HX-Trigger events bubble Add FAQ or explanaition on how HX-Trigger events bubble Nov 15, 2020
@1cg 1cg closed this as completed Aug 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

4 participants