Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcus-at-localhost/a1092a4683730603fa8aeeee20d5b178 to your computer and use it in GitHub Desktop.
Save marcus-at-localhost/a1092a4683730603fa8aeeee20d5b178 to your computer and use it in GitHub Desktop.
<form hx-post="/example" hx-ext="disable-submit">
  <button type="submit" class="hx-disable button-default">Save</button>
</form>
htmx.defineExtension('disable-submit', {
  onEvent: function (name, evt, data) {
    let elt = evt.detail.elt;
    let result = elt.querySelectorAll(".hx-disable");

    if (name === "htmx:beforeRequest") {
      result.forEach(element => element.disabled = true);
      if (elt.classList.contains("hx-disable")) elt.disabled = true;

    } else if(name == "htmx:afterRequest") {
      result.forEach(element => element.disabled = false);
      if (elt.classList.contains("hx-disable")) elt.disabled = false;
    }
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment