Skip to content

How to use response HX-Trigger to trigger the event on a sibling element? #2699

Answered by Telroshan
renannprado asked this question in Q&A
Discussion options

You must be logged in to vote

Hey, hx-on is equivalent to calling addEventListener on the element that you put the attribute on, so it cannot be listening "outside" as hx-trigger does. In this case you indeed want to put the listener on the element that will receive the event, be it the body if you need a from:body equivalent.
When your client logic gets specific enough, it may be worth considering using a simple script of your own and calling addEventListener in it!

And if you really want to stick to hx-on, I imagine a dirty trick could be declaring a hx-on on the parent (that receives the trigger header), that queries all of its children and fires another custom event of yours on them, which would then be listened o…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@renannprado
Comment options

@Telroshan
Comment options

@renannprado
Comment options

@Telroshan
Comment options

Answer selected by renannprado
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants