https://htmx.org logo
Join Discord
Powered by
# htmx-general
  • a

    agreeable-apartment-19546

    01/25/2023, 12:06 AM
    Thanks. I have a form that POSTs to my API and a JSON response is returned with
    HX-Trigger: spot_successful
    in the headers. I've confirmed in the dev tools that the header is present in the POST response ( although it's lowercase,
    hx-trigger
    ). In the HTML itself, I have tried both
    document.body.addEventListener
    and
    htmx.on
    and neither seem to trigger. Example :
    Copy code
    htmx.on("spot_successful", function (event) {
            alert("SUCCESS");
            toast.classList.remove("hidden");
            setTimeout(function () {
                toast.classList.add("hidden");
            }, 3000);
        });
    I'm just trying to have a toast appear and go away again.
  • m

    mysterious-toddler-20573

    01/25/2023, 12:10 AM
    how is the form POST-ing, via htmx?
  • a

    agreeable-apartment-19546

    01/25/2023, 12:11 AM
    No, I've had to make some changes to the payload so I'm calling
    xhr.send
    on a
    XMLHttpRequest
  • a

    agreeable-apartment-19546

    01/25/2023, 12:11 AM
    Is that the piece I'm missing ?
  • m

    mysterious-toddler-20573

    01/25/2023, 12:44 AM
    ah, yeah
  • m

    mysterious-toddler-20573

    01/25/2023, 12:45 AM
    htmx has the logic that handles the response header, so you'll need to do so yourself
  • m

    mysterious-toddler-20573

    01/25/2023, 12:45 AM
    if you are issuing the request w/ your own code
  • a

    agreeable-apartment-19546

    01/25/2023, 12:45 AM
    Ah, okay, so that explains it. I think I saw somewhere some htmx syntax to, say, modify the payload before sending -- could I use that and have htmx still issue the POST ?
  • a

    agreeable-apartment-19546

    01/25/2023, 12:46 AM
    Alternatively, how might I "do it myself" if I want to stay with my own XML request ? Perhaps a dumb question, but this is my first foray into anything frontend so any pointers much appreciated ๐Ÿ™‚
  • m

    mysterious-toddler-20573

    01/25/2023, 12:47 AM
    Yes, you can hook in to the
    htmx:configRequest
    event and modify parameters, headers, etc: https://htmx.org/events/#htmx:configRequest
  • a

    agreeable-apartment-19546

    01/25/2023, 12:47 AM
    That was the one ! Thanks
  • a

    agreeable-apartment-19546

    01/25/2023, 12:49 AM
    Looks like I could rewrite
    detail.parameters
    to the payload I want perhaps
  • l

    late-king-98305

    01/25/2023, 2:03 AM
    โ˜๏ธ top-tier advice here!
  • l

    late-king-98305

    01/25/2023, 2:13 AM
    This uses Bootstrap's modals - https://github.com/bit-badger/myPrayerJournal/blob/main/src/MyPrayerJournal/Views/Journal.fs - it's F#, but hopefully a few pointers will help. Line 30 is where I have the button that triggers the modal, and its
    hx-target
    (
    _hxTarget
    ) is a modal area. The code line 30 is a part of is rendered multiple times per page, but the modal target area is rendered once per page, starting in line 98; the actual
    #snoozeBody
    target is on line 109, and ends up being rendered initially as an empty
    div
    . The content of that body is filled in line 172 and following, based on the URL fired off by the button click. For closing it once processing is complete, I return a header from the server, then fire off the
    click
    event of the close button when that header. That's in https://github.com/bit-badger/myPrayerJournal/blob/main/src/MyPrayerJournal/wwwroot/script/mpj.js - the handler starts on line 85, and the part that responds to the header is lines 91-94. Hope that helps; if not, let us know what questions you might have about it.
  • s

    salmon-church-58191

    01/25/2023, 3:35 AM
    @late-king-98305 Thanks! I'll take a look at the modal... it will probably do the trick.
  • b

    billions-easter-81130

    01/25/2023, 8:06 AM
    If it's unclear what I meant, I can try to rephrase in a hopefully more meaningful way. ๐Ÿ™‚
  • b

    bland-coat-6833

    01/25/2023, 9:26 AM
    Some table weirdness: I have some `hx-get`s in my table header that target the table. These work as expected. I then have an
    hx-delete
    in the actual table itself. These have the same target but the response gets added to the table content unless I specify an
    hx-swap='innerHTML'
    .
  • b

    bland-coat-6833

    01/25/2023, 9:26 AM
    Seems odd. If I leave out the
    hx-swap
    then I also get an error in the console
    ignore that. I think that's the SSE connection having a blip
  • g

    glamorous-honey-42686

    01/25/2023, 11:05 AM
    Hi! We use boost quite a lot, but I'm wondering on how to deal with JS that creates objects when certain elements are on a page. Ideally I would like to clean up when navigating away. Is there any "best practices" for this kind of stuff?
  • o

    orange-umbrella-16693

    01/25/2023, 12:18 PM
    https://twitter.com/hungrybluedev/status/1617868037323096064?s=20&t=thNL3FJfrUJpwIkdUa9NxA
  • o

    orange-umbrella-16693

    01/25/2023, 12:19 PM
    The virgin "dynamically loaded js components so your application can be O(1)!" vs The Chad "just use hypermedia lol"
  • f

    freezing-waitress-26396

    01/25/2023, 1:19 PM
    return to framesets
  • f

    freezing-waitress-26396

    01/25/2023, 1:21 PM
    ๐ŸชŸ
  • f

    fancy-elephant-10660

    01/25/2023, 1:26 PM
    I'm using hx-push-url with some pagination on an index page. When click on that pagination it swaps out the current list with a new list. When I click on one of the item I go to the detail page. However when clicking on the back button it will only load the partial template. Is there some header to know if user clicked back button?
  • b

    boundless-vase-80440

    01/25/2023, 1:32 PM
    do you push url when the user clicks on a page?
  • f

    fancy-elephant-10660

    01/25/2023, 1:32 PM
    yes
  • b

    boundless-vase-80440

    01/25/2023, 1:33 PM
    and again when the user clicks on an item?
  • b

    boundless-vase-80440

    01/25/2023, 1:36 PM
    hx-push-url should push a snapshot of the entire DOM to localstorage. if you push the page URL and the item URL, it should restore the whole page
  • b

    boundless-vase-80440

    01/25/2023, 1:37 PM
    from the docs:
    Copy code
    The hx-push-url attribute allows you to push a URL into the browser location history. This creates a new history entry, allowing navigation with the browserโ€™s back and forward buttons. htmx snapshots the current DOM and saves it into its history cache, and restores from this cache on navigation.
  • b

    boundless-vase-80440

    01/25/2023, 1:39 PM
    by default the body tag is saved in localstorage. you can override that with
    hx-history-elt
1...996997998...1146Latest