<newbie> Communicate only with nearest ancestor #996
-
(Hi there - I'm just picking this up so I'm almost certainly doing something wrong, in which case please tell me the more idiomatic approach. I'm not wedded to my current approach at all) I have an app which can have a stack of windows on top of each other (don't ask why - just accept this part please). Each window has a close button which should close that window, i.e. the nearest <div class="window1">
<div class="window2">
...
<button>to close window2</button>
</div> -- window2
<button>to close window1</button>
</div> -- window1 clicking the button for Approaches I might try without htmx:
I like the approach in https://htmx.org/examples/modal-custom/ which basically has the button fire an event which the window div listens to, but how do I restrict the parent window divs from listening? I hope that makes sense - thanks for any/all suggestions |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @yatesco , Normally I would say that your question is a bit of htmx's scope, because it implies Javascript for pure-client-side interaction (closing a modal), and htmx is about client-server communication and managing application state through HTML's hypertext superpowers. This is why the example you provide uses hyperscript (the So normally my answer would be "just plug in some JS logic in there, like the example does, but a bit smarter, maybe with your idea of a random ID for each window, which could be generated on server-side and thus injected in the HTML via a data-attribute", etc. But I've come with a hacky thing not so long ago using htmx, exactly for this use-case (I have no nested modals because my design team rocks 😄, but it also works when you have only one), with zero custom JS involved:
What do you think? |
Beta Was this translation helpful? Give feedback.
Hi @yatesco ,
Normally I would say that your question is a bit of htmx's scope, because it implies Javascript for pure-client-side interaction (closing a modal), and htmx is about client-server communication and managing application state through HTML's hypertext superpowers. This is why the example you provide uses hyperscript (the
_="on click trigger closeModal"
part of the "close" button of the modal).So normally my answer would be "just plug in some JS logic in there, like the example does, but a bit smarter, maybe with your idea of a random ID for each window, which could be generated on server-side and thus injected in the HTML via a data-attribute", etc.
But I've come with a hacky…