Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
If all checkboxes are checked enable submit button

I have a simple form with some checkboxes and I'm trying to get it so that the submit button is enabled when all the checkboxes are checked.

I've been messing with some of the answers from here: Enable submit button only when all fields are filled

But it seems like it either doesn't enable the submit button or it enables it right after one is checked. Here's what I have:

$(document).on("change keyup", ".required", function(e) {
  let Disabled = true;
  $(".required").each(function() {
    let value = this.value;
    if (value && value.trim() != "") {
      Disabled = false;
    } else {
      Disabled = true;
      return false;
    }
  });

  if (Disabled) {
    $(".toggle-disabled").prop("disabled", true);
  } else {
    $(".toggle-disabled").prop("disabled", false);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="#">
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <button type="button" class="toggle-disabled" disabled>Submit</button>
  </p>
</form>

It's weird cause it sorta works, but as soon as I check one box the submit button is enabled. Looks like I have all the pieces there, but not sure if it's something within the change keyup or something else.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

  1. You only need an EventListener to check for changes made within the input (checkboxes). You do not need to listen to all changes or keyups on the entire document. So just use $('form input[type="checkbox"]').change(function(){ ... } which going to save resources.
  2. You have to check how many checkboxes have not been checked with $('form input[type="checkbox"]:not(:checked)').length. This will give the exact number of checkboxes that are currently unchecked.
  3. Now you can use an if/else-statement to see if that count is higher than 0 (which means that at least 1 checkbox is still unchecked. In my example I replaced the if/else-statement with a conditional ternary operator to save a few lines:

$('form input[type="checkbox"]').change(function(){
  var allChecked = !!$('form input[type="checkbox"]:not(:checked)').length;
  $('.toggle-disabled').prop('disabled', allChecked);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="#">
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <button type="button" class="toggle-disabled" disabled>Submit</button>
  </p>
</form>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You have to keep track of count as suggested by @tacoshy, I have modified your code to reflect the changes.

$(document).on("change keyup", ".required", function(e) {
  let Disabled = $(".required").length;
  $(".required").each(function() {
    if(this.checked) Disabled--;
  });

  if (Disabled) {
    $(".toggle-disabled").prop("disabled", true);
  } else {
    $(".toggle-disabled").prop("disabled", false);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="#">
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <button type="button" class="toggle-disabled" disabled>Submit</button>
  </p>
</form>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Just compare checked and not checked counts on any change.

  • Give the form an id (event.delegateTarget)
  • count all of the checkboxes (filter for that type)
  • count checked checkboxes
  • compare and set the property based on that
  • scope to the form for the button (might consider filter by input[type="submit"] also
  • trigger on load to set it disabled if not all checked at that point

$("#formid").on("change", ".required", function(event) {
  let allCheckboxes = $(event.delegateTarget)
    .find(".required").filter('input[type="checkbox"]');
  let checkedCount = allCheckboxes.filter(":checked").length;
  $(event.delegateTarget).find(".toggle-disabled")
    .prop("disabled", allCheckboxes.length != checkedCount);
}).trigger("change");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="formid" action="#">
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <button type="button" class="toggle-disabled" disabled>Submit</button>
  </p>
</form>

Alternative

$("#formid").on("change", ".required", function(event) {
  let notAllChecked = !!$(event.delegateTarget)
    .find(".required").filter('input[type="checkbox"]:not(:checked)').length;
   $(event.delegateTarget).find(".toggle-disabled")
    .prop("disabled", notAllChecked);
}).trigger("change");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="formid" action="#">
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" class="filled-in box required" name="item" />
      <span>Filled in</span>
    </label>
  </p>
  <p>
    <button type="button" class="toggle-disabled" disabled>Submit</button>
  </p>
</form>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda