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

    broad-pencil-64390

    11/21/2022, 6:06 PM
    Copy code
    Failed to execute 'querySelector' on 'Element': '#.field-status' is not a valid selector.
    Can
    hx-select-oob
    only be used to select ids? I got the above error when having
    hx-select-oob=".field-status"
  • b

    broad-pencil-64390

    11/21/2022, 6:12 PM
    Aha maybe this is because
    .field-status
    is a child of the
    hx-target
    🤔 ..even though hx-swap is using
    afterbegin
  • t

    thankful-cat-54263

    11/21/2022, 7:20 PM
    Heya. I fear I'm doing something wrong: I've got a server-side processing step that compiles a piece of code, and records the various stages. now, I'd like to have a UI for that using htmx, and that'll show a select box for the various stages, and display the stage's intermediate result. right now, each change in the select (choosing another stage) will re-process the code in the backend, produce the same state, and return a different view. I'd like to avoid the re-processing, but I'm afraid I don't know how. (before, I had attached the intermediate stage to the select option via some listener, and had a click on the option update the viewer.)
    m
    • 2
    • 13
  • q

    quick-crayon-19412

    11/21/2022, 9:41 PM
    Hi there, I found a great example of some form validation using Bootstrap (https://gist.github.com/marcus-at-localhost/c9f0a34a2e19d666fd95f61f002ea516) but possibly running into an issue with the trigger when defining a custom extension. It kind of works since it shows which fields are invalid for a second but it is still saying the form submission is successful and refreshes the page.
    Copy code
    html
    <form sprig s-action="entries/save-entry" s-method="post" hx-ext="bs-validation" class="needs-validation" novalidation>
    ...
    <input id="customField" name="fields[customField]" type="text" required />
    </form>
    Copy code
    js
    htmx.defineExtension('bs-validation', {
      onEvent: function (name, evt, data) {
    
        if (name !== "htmx:afterProcessNode") {
            return;
        }
    
        let form = evt.detail.elt;
        // check if trigger attribute and submit event exists
        // for the form
        if(!form.hasAttribute('hx-trigger')){
          // set trigger for custom event bs-send
          form.setAttribute('hx-trigger','bs-send');
          // and attach the event only once
          form.addEventListener('submit', function (event) {
            if (form.checkValidity()) {
              // trigger custom event hx-trigger="bs-send"
              htmx.trigger(form, "bsSend");
            }
    
            // focus the first invalide field
            let invalidField = form.querySelector(':invalid');
            if(invalidField) {
              invalidField.focus();
            }
    
            event.preventDefault()
            event.stopPropagation()
    
            form.classList.add('was-validated')
          }, false)  
        }
      }
    });
  • b

    busy-tomato-43957

    11/21/2022, 10:52 PM
    So I ended up making that todo app. I built it using Rust as a backend language. But instead of hosting it on a server, it's compiled to WASM, and run inside the user's browser in a ServiceWorker (because why not?). The initial prototype was done by richardanaya, and I built on top of it to experiment both with Rust and htmx. Check it out: https://richardanaya.github.io/wasm-service/
  • b

    busy-tomato-43957

    11/21/2022, 10:59 PM
    To be clear I found this design has a number of issues, including the fact that the browser eagerly reaps the SW so it loses its state if you leave the tab for a minute or so. But leaving such minor inconveniences aside, technically this is one way you could add offline capabilities to htmx!
  • b

    brainy-ice-92385

    11/22/2022, 12:51 AM
    Neat! I've been considering this for a while myself.
  • b

    busy-tomato-43957

    11/22/2022, 12:56 AM
    Yeah the correct solution to storing data in volatile memory is to... store it in persistent memory instead lol. Yes you could use indexeddb from ServiceWorker (not localstorage, I think SWs are restricted from using that) to make it persistent, but you'll have to add bindings to call back into JS (or switch to wasm-bindgen)
  • b

    brainy-ice-92385

    11/22/2022, 12:57 AM
    Still cool as heck
  • b

    busy-tomato-43957

    11/22/2022, 12:58 AM
    Another issue is that the SW is not restarted after its been reaped, even if you start interacting with the page again. Not sure what it's doing wrong or how to fix it
  • b

    boundless-leather-51644

    11/22/2022, 10:21 AM
    Hi
  • b

    boundless-leather-51644

    11/22/2022, 10:22 AM
    Is there an example covering
    htmx:responseError
    ?
  • b

    boundless-vase-80440

    11/22/2022, 12:22 PM
    only this https://htmx.org/docs/#requests
  • b

    boundless-vase-80440

    11/22/2022, 12:22 PM
    and this: https://htmx.org/events/#htmx:responseError
  • s

    salmon-oil-67352

    11/22/2022, 8:42 PM
    I'm having some issues with "hx-encoding='multipart/form-data' ". Whenever i have a form with this attribute, the page reloads after the form is submitted. This is my form:
    Copy code
    <form
        hx-encoding='multipart/form-data' 
        hx-post="{% url 'invoices_to_be_processed_upload' %}"
        hx-target="#invoice_to_be_processed_table"
    >
        <input type='file' name='invoice_file' multiple>
        <button type="submit">
            {% trans "Upload" %}
        </button>
    </form>
    Is this normal?
  • m

    mysterious-toddler-20573

    11/22/2022, 9:07 PM
    no
  • m

    mysterious-toddler-20573

    11/22/2022, 9:08 PM
    this is the only place we use that attribute: https://github.com/bigskysoftware/htmx/blob/e2ecde0cb1de60223fdeb67101978a60c0e4a8ee/src/htmx.js#L2401
  • m

    mysterious-toddler-20573

    11/22/2022, 9:08 PM
    we do add an additional header to the request
  • m

    mysterious-toddler-20573

    11/22/2022, 9:09 PM
    and we use a FormData object, rather than a string to collect values. Can you look in the console and see if there is an exception that is maybe happening and causing the javascript handler to die?
  • s

    salmon-oil-67352

    11/22/2022, 9:16 PM
    No exception in the console
  • s

    salmon-oil-67352

    11/22/2022, 9:17 PM
    when i remove "hx-encoding='multipart/form-data'" the form is submitted, and the page does not reload
  • s

    salmon-oil-67352

    11/22/2022, 9:17 PM
    The backend is Django with the classic response:
    Copy code
    return render(
        request,
        "partials/invoice_to_be_processed_table.html",
        context={
            "invoices": invoices,
        },
    )
  • s

    salmon-oil-67352

    11/22/2022, 9:18 PM
    HTMX 1.8.4
  • s

    salmon-oil-67352

    11/22/2022, 9:24 PM
    It's very curious, everything works as expected, the hx-target gets updated correctly after the request, and about 1/2 seconds later, the page refreshes
  • m

    mysterious-toddler-20573

    11/22/2022, 9:31 PM
    Is there anything else going on in the page script-wise?
  • s

    salmon-oil-67352

    11/22/2022, 9:38 PM
    Oh wow i found the issue, i'm using https://github.com/adamchainz/django-browser-reload Basically what this does is it reloads your page when you make a change to your codebase. And when i upload a file with HTMX locally, the file goes to my /media folder. And django-browser-reload, detects this new file and thinks it's a change and then reloads the page.. Thanks for your help 👍
  • m

    mysterious-toddler-20573

    11/22/2022, 9:39 PM
    oof
  • b

    brash-house-85887

    11/22/2022, 11:16 PM
    If I don't have a hx-swap-oob in my response then a
    <script language="javascript">
    with a
    htmx.on('htmx:afterSettled'..)
    works as expected. But if I do include an hx-swap-oob div (in addition to the normal, non-swap-oob div) then it never gets called regardless of where I put it in the response. Is there something I'm missing?
  • m

    mysterious-toddler-20573

    11/22/2022, 11:19 PM
    that sounds like a bug
  • m

    mysterious-toddler-20573

    11/22/2022, 11:20 PM
    are you able to give me the basic content?
1...919920921...1146Latest