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

146
Vistas
How do I count how many checkboxes are selected on a page with jQuery

I want to count how many checkboxes on my page are selected using jQuery. I've written the following code:

      var numberOfCheckboxesSelected = 0;
      $(':checkbox').each(function(checkbox) {
          if (checkbox.attr('checked'))
              numberOfCheckboxesSelected++;
      });

However, my code errors out with the message "Object doesn't support this property or method" on the third line.

How do I count how many checkboxes are selected on my page?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

jQuery supports the :checked pseudo-selector.

var n = $("input:checked").length;

This will work for radio buttons as well as checkboxes. If you just want checkboxes, but also have radio buttons on the page:

var n = $("input:checkbox:checked").length;
over 4 years ago · Santiago Trujillo Denunciar

0

Try this(to avoid counting any selected radio buttons):

var numberOfCheckboxesSelected = $('input[type=checkbox]:checked').length; 
over 4 years ago · Santiago Trujillo Denunciar

0

The first argument passed to the callback in each is the index, not the element. You should use this or a second argument:

$(':checkbox').each(function(idx, checkbox) {
      if (checkbox.attr('checked'))
          numberOfCheckboxesSelected++;
});

Moreover, the easier way to do this is to select only the checked elements with the :checked selector and then check the selection's length property:

var numberOfCheckboxesSelected = $('input:checkbox:checked').length;
over 4 years ago · Santiago Trujillo 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