1

I have created a form with a few inputs and a button using zurb foundation, is it possible to disable the button till all the fields of the form are being attended ?

user3025288
  • 39
  • 1
  • 10

1 Answers1

1

You can identify all your required inputs (with a class) and then when any of them changes or get focusout-ed check if there are empty ones. If there all filled, then enable the button.

// Bind the events to the inputs
// You can use any class nedded, this covers selects too
// You even can add more events to suite your needs
$('.input-required').on('focusout change', function() {
  // We instantiate a variable to hold the button status
  var buttonDisabled = false;
  // Iterate every input
  $('.input-required').each(function() {
      // If there is any empty...
      if (!$(this).val()) {
        // We say true to the disableness of the button
        buttonDisabled = true;
      }
    })
    // Set the status to the button
  $('button').prop('disabled', buttonDisabled);
});
form {
  width: 460px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<form action="#">
  <input type="text" class="input-required">
  <input type="text" class="input-required">
  <input type="text" class="input-required">
  <input type="text" class="input-required">
  <input type="text" class="input-required">
  <input type="text" class="input-required">

  <hr>
  <button type="submit" disabled>Process</button>
</form>
Alex Quintero
  • 1,160
  • 10
  • 21
  • I am sorry .. I have already created html portion of it and I do not know how the JS working for it. – user3025288 Oct 02 '14 at 19:22
  • @user3025288 Yes, I'm sorry, the answer was too simplistic. There is a revised version that should work with your markup ;) – Alex Quintero Oct 02 '14 at 19:35
  • Sorry for the misunderstanding caused by me. This helped me a lot is there any why that I can write the same function in a different page and call it ? – user3025288 Oct 03 '14 at 13:36
  • @user3025288 Like in another file? There is no problem with that, in fact is recommended to have a `main.js` like file to contain all global functions ;) – Alex Quintero Oct 03 '14 at 13:42
  • yes!.. so I have moved the JavaScript code to a file and how do I call it using action ? your logic is write but I have someother problem implementing it. – user3025288 Oct 03 '14 at 13:54
  • In that case I recommend you to place the script tag [before the `

    ` tag](http://stackoverflow.com/questions/7751514/javascript-in-head-or-just-before-body) and wrap it in a [jQuery ready()](http://api.jquery.com/ready/) and delegate the event from the body, replacing the first line with `$('body').on('focusout change', '.input-required', function() {`

    – Alex Quintero Oct 03 '14 at 14:03
  • 1
    Thanks a lot @AlexQuintero I would have not done it without you help.Code worked without making the above changes (and to be frank I have not worked on Jquery :( so I could not understand what the above statement meant ) anyways thanks :) – user3025288 Oct 03 '14 at 14:16