| 1 | --- |
| 2 | title: "hx-disable" |
| 3 | description: "Disables elements during the request" |
| 4 | --- |
| 5 | |
| 6 | The `hx-disable` attribute allows you to specify elements that will have the `disabled` attribute |
| 7 | added to them for the duration of the request. |
| 8 | |
| 9 | ## Syntax |
| 10 | |
| 11 | ```html |
| 12 | <button hx-post="/submit" hx-disable="this">Submit</button> |
| 13 | ``` |
| 14 | |
| 15 | The value of this attribute can be: |
| 16 | |
| 17 | * A CSS query selector of the element to disable. |
| 18 | * `this` to disable the element itself |
| 19 | * `closest <CSS selector>` which will find the [closest](https://developer.mozilla.org/docs/Web/API/Element/closest) |
| 20 | ancestor element or itself, that matches the given CSS selector |
| 21 | (e.g. `closest fieldset` will disable the closest to the element `fieldset`). |
| 22 | * `find <CSS selector>` which will find the first child descendant element that matches the given CSS selector |
| 23 | * `next` which resolves |
| 24 | to [element.nextElementSibling](https://developer.mozilla.org/docs/Web/API/Element/nextElementSibling) |
| 25 | * `next <CSS selector>` which will scan the DOM forward for the first element that matches the given CSS selector |
| 26 | (e.g. `next button` will disable the closest following sibling `button` element) |
| 27 | * `previous` which resolves |
| 28 | to [element.previousElementSibling](https://developer.mozilla.org/docs/Web/API/Element/previousElementSibling) |
| 29 | * `previous <CSS selector>` which will scan the DOM backwards for the first element that matches the given CSS selector. |
| 30 | (e.g. `previous input` will disable the closest previous sibling `input` element) |
| 31 | |
| 32 | Here is an example with a button that will disable itself during a request: |
| 33 | |
| 34 | ```html |
| 35 | <button hx-post="/example" hx-disable="this"> |
| 36 | Post It! |
| 37 | </button> |
| 38 | ``` |
| 39 | |
| 40 | When a request is in flight, this will cause the button to be marked with [the |
| 41 | `disabled` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled), |
| 42 | which will prevent further clicks from occurring. |
| 43 | |
| 44 | The `hx-disable` attribute also supports specifying multiple CSS selectors separated by commas to disable multiple |
| 45 | elements during the request. Here is an example that disables buttons and text input fields of a particular form during |
| 46 | the request: |
| 47 | |
| 48 | ```html |
| 49 | <form hx-post="/example" hx-disable="find input[type='text'], find button"> |
| 50 | <input type="text" placeholder="Type here..."> |
| 51 | <button type="submit">Send</button> |
| 52 | </form> |
| 53 | ``` |
| 54 | |
| 55 | Note that you can also use the `merge` modifier to merge parent values for a disabled elements and add additional |
| 56 | disabled element CSS selectors: |
| 57 | |
| 58 | ```html |
| 59 | <main hx-disable="#logout-button"> |
| 60 | ... |
| 61 | <form hx-post="/example" hx-disable:merge="find input[type='text'], find button"> |
| 62 | <input type="text" placeholder="Type here..."> |
| 63 | <button type="submit">Send</button> |
| 64 | </form> |
| 65 | </main> |
| 66 | ``` |