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

    lemon-rocket-71964

    12/08/2022, 7:42 PM
    document.body.addEventListener('htmx:beforeCleanupElement', function(evt) { evt.preventDefault();} The above do I need to add to index.html?
  • m

    mysterious-toddler-20573

    12/08/2022, 7:45 PM
    Copy code
    html
    <script>
      htmx.on("htmx:beforeCleanupElement", function(evt){
        console.log("here", evt.target);
        evt.preventDefault();
      })
    </script>
  • m

    mysterious-toddler-20573

    12/08/2022, 7:45 PM
    add that at the top of the test page
  • m

    mysterious-toddler-20573

    12/08/2022, 7:45 PM
    along w/ the modification above
  • m

    mysterious-toddler-20573

    12/08/2022, 7:46 PM
    and try w/
    evt.preventDefault()
    commented out and then with it in, and see if the number of "here" printed goes way, way down
  • m

    mysterious-toddler-20573

    12/08/2022, 7:46 PM
    (sorry this is so janky)
  • m

    mysterious-toddler-20573

    12/08/2022, 7:47 PM
    https://github.com/bigskysoftware/htmx/issues/724
    r
    g
    +3
    • 6
    • 29
  • l

    lemon-rocket-71964

    12/08/2022, 7:51 PM
    I tried this code by commenting evt.preventDefault(). But it does not print anything on the console. Am I missing something?
  • m

    mysterious-toddler-20573

    12/08/2022, 7:52 PM
    Mmmm
  • l

    lemon-rocket-71964

    12/08/2022, 7:54 PM
    htmx htmx.on("htmx:beforeCleanupElement", function (evt) { console.log("here", evt.target); // evt.preventDefault(); }) Click me
  • m

    mysterious-toddler-20573

    12/08/2022, 7:54 PM
    Ah, you need it in a new script tag.
  • m

    mysterious-toddler-20573

    12/08/2022, 7:54 PM
    You can't have a script w/ a
    src
    attribute as well as code in it.
  • l

    lemon-rocket-71964

    12/08/2022, 7:57 PM
    oh okay. I tried the following still no luck htmx htmx.on("htmx:beforeCleanupElement", function (evt) { console.log("here", evt.target); // evt.preventDefault(); })
  • l

    lemon-rocket-71964

    12/08/2022, 8:14 PM
    Sorry my bad I haven't removed the bubble code. Now it printing all the entries. I tried w/ and w/o evt.preventDefault(). But prints all the entries in the console.log
  • a

    adorable-fish-28954

    12/09/2022, 3:55 AM
    Hey 🙂 should i be able to run
    Copy code
    if (typeof variable !== 'undefined') {
        // the variable is defined
    }
    in a being swapping in via HTMX? I am getting a htmx js error saying 'cannot access 'variable' before initialization'
  • a

    adorable-fish-28954

    12/09/2022, 3:56 AM
    but the whole point of that is to check if its initialized
  • f

    freezing-controller-74032

    12/09/2022, 9:41 AM
    Hi. Please help me. How i can show json responce by key ? Example I get response {'result': false, 'html-code': 'Test'} I select target to show answer like html hx-target='#div_id' Butt he show all json. How can i show only json['html-code'] ?
  • m

    mysterious-toddler-20573

    12/09/2022, 2:03 PM
    https://twitter.com/htmx_org/status/1601215756347518978
  • m

    mysterious-toddler-20573

    12/09/2022, 2:04 PM
    you are going to need to transform the response to plain HTML this is complicated given what it looks like you are trying to do could you just return the HTML itself, without the JSON?
  • m

    mysterious-toddler-20573

    12/09/2022, 2:04 PM
    no good reason that shouldn't work 🤔
  • m

    mysterious-toddler-20573

    12/09/2022, 2:05 PM
    OK, for your case and for now, I would say just comment that event out. I think I threw it in as a nice to have, but if there are perf issues it should at least sit behind an option
  • l

    lemon-rocket-71964

    12/09/2022, 3:48 PM
    okay I will comment that out for now. If you think of any other better solution please share with me. Thank you for great support !!!
  • l

    late-king-98305

    12/09/2022, 4:53 PM
    I think you can do
    variable === undefined
    without requiring JS to run
    typeof
    on it, to accomplish the same result. (I could also be wrong, though; YMMV and all that...)
  • g

    gorgeous-airport-54386

    12/09/2022, 4:53 PM
    Copy code
    js
    if ('variable' in window)
    ?
  • f

    freezing-controller-74032

    12/09/2022, 5:00 PM
    Yes you are right. I just need to return the html and generate the result in the backend. Help with another question please. HТМХ Allows you to change block style by ID ? For example div style I want to change it to or replace . How to do it ?
  • m

    mysterious-toddler-20573

    12/09/2022, 5:01 PM
    if it is purely client side (you don't want to make a server request) then scripting is the right thing. Vanilla js, alpine.js and hyperscript are popular options for htmx users.
  • f

    freezing-controller-74032

    12/09/2022, 5:03 PM
    i want use request but i dont undestand what responce i must return for change div style
  • m

    mysterious-toddler-20573

    12/09/2022, 5:04 PM
    if you are replacing the div just render it with the new style
  • m

    mysterious-toddler-20573

    12/09/2022, 5:04 PM
    it sounds like you might want to use
    hx-swap="outerHTML"
    if you are targeting that div specifically, and re-render the entire div.
  • f

    freezing-controller-74032

    12/09/2022, 5:06 PM
    Thanks a lot. The last question, is it possible in this way to replace not one but several separate blocks with one answer?
1...938939940...1146Latest