Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HTMX on a web form, how do I grey out the button while a request is processed or the form fields are not filled?

Tags:

htmx

I'm trying to learn HTMX https://htmx.org/ by building a simple example.

This here is a form with two data entry fields.

It should fire, when the "search" button is clicked or enter is pressed.

This works.

I would like to provide some feedback to the form user:

  1. to grey out the "search" button so it's only activated when there's data in both fields

  2. to grey out the "search" button while a request to the backend it curently in progress. Alternatively show a spinner and block input while the request runs.

     <html>
    
     <head>
         <title>example</title>
         <script src="https://unpkg.com/[email protected]"
                 integrity="sha384-EzBXYPt0/T6gxNp0nuPtLkmRpmDBbjg6WmCUZRLXBBwYYmwAUxzlSGej0ARHX0Bo"
                 crossorigin="anonymous"></script>
    
     </head>
    
     <body>
     <h1>Hello World!!!</h1>
    
     <div id="main">
         <button hx-get="/button1" hx-target="#main">
             Get stuff
         </button>
     </div>
    
     <div id="main2">
         <form>
             <label for="search">label:</label>
             <input id="s1" name="q" type="search" placeholder="one">
             <input id="s2" name="q2" type="search" placeholder="two">
             <button hx-post="/button2" hx-target="#main" hx-trigger="click, keyup[enterKey] from:body">
                 Search
             </button>
         </form>
     </div>
    
     </body>
     </html>
    
like image 334
576i Avatar asked Oct 28 '25 02:10

576i


1 Answers

This should be easily done by using htmx extensions.

Check the following:

disable-element

loading-states

Make sure to use the latest htmx version.

To get the list of all htmx extensions you can check:

https://extensions.htmx.org/

like image 53
mariodev Avatar answered Oct 30 '25 11:10

mariodev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!