Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: disable-element extension handles multiple elements from CSS selector #1492

Open
ehenighan opened this issue Jun 13, 2023 · 2 comments

Comments

@ehenighan
Copy link
Contributor

Kind of goes hand in hand with #1491 really - the disable-element extension for when a request is in flight accepts a CSS selector, but if you used e.g. a class name in that selector you could potentially find multiple target elements.

Use case is, I might want to disable multiple other actions (e.g. buttons) while my request is in flight, not just a single one by ID.

@andryyy
Copy link

andryyy commented Jun 13, 2023

I use https://htmx.org/extensions/loading-states/ for similar things.

@GPla
Copy link

GPla commented Sep 10, 2023

The needed change should be pretty simple. In the current state, the extension uses querySelector to select the first that matches the selector. Instead querySelectorAll could be used like this:

"use strict";

// Disable Submit Button
htmx.defineExtension('disable-element', {
    onEvent: function (name, evt) {
        let elt = evt.detail.elt;
        let target = elt.getAttribute("hx-disable-element");
        let targetElements = (target == "self") ? [elt] : document.querySelectorAll(target);

        targetElements.forEach((targetElement) => {
            if (name === "htmx:beforeRequest" && targetElement) {
                targetElement.disabled = true;
            } else if (name == "htmx:afterRequest" && targetElement) {
                targetElement.disabled = false;
            }
        })
    }
});

EDIT: there is already a pull request #1650 that implements this change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants