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

191
Vistas
Want to get the value of checkbox and assign it's value to the class-name

In a div, I have a 'select all' checkbox. When this checkbox is checked, all the other checkboxes will automatically checked. I want to assign their values as a class name. I am able to achieve this when a single checkbox is checked. But now I want this should work when all checkbox is checked at once when a select-all checkbox is checked. ​

$('.select-all').on('click', function() {
  let isSelected = $(this).is(':checked');
  $(this).parents('.checkbox-list').find('input[type="checkbox"]').not('.select-all').each(function() {
    if (isSelected) {
      $(this).prop('checked', true);
    } else {
      $(this).prop('checked', false);
    }
  })
});

$('input:checkbox').not('.select-all').change(function() {
  var cl = $(this).val();
  var cls = 'abc' + '' + cl + '';
  if ($(this).is(':checked')) {
    $(this).addClass(cls);
  } else {
    $(this).removeClass(cls);
  }
})
<div class="checkbox-list">
  <label><input type="checkbox" class="select-all" name="">All</label>
  <label><input type="checkbox" name="" value="1">One</label>
  <label><input type="checkbox" name="" value="2">Two</label>
  <label><input type="checkbox" name="" value="3">Three</label>
  <label><input type="checkbox" name="" value="4">Four</label>
  <label><input type="checkbox" name="" value="5">Five</label>
  <label><input type="checkbox" name="" value="6">Six</label>
  <label><input type="checkbox" name="" value="6">Three</label>
  <label><input type="checkbox" name="" value="7">Four</label>
  <label><input type="checkbox" name="" value="8">Five</label>
  <label><input type="checkbox" name="" value="9">Six</label>
</div>

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

0

You pretty much wrote the code already to achieve this, What i did here is adding the cl = $(this).val(); and cls = 'abc' + '' + cl + ''; in the each loop and add the class when isSelected is true, and delete when false.

$('.select-all').on('click', function() {
    let isSelected = $(this).is(':checked');
    $(this).parents('.checkbox-list').find('input[type="checkbox"]').not('.select-all').each(function() {
        let cl = $(this).val();
        let cls = 'abc' + '' + cl + '';
        if (isSelected) {
            $(this).prop('checked', true);
            $(this).addClass(cls);
        } else {
            $(this).prop('checked', false);
            $(this).removeClass(cls);
        }
    })
});

$('input:checkbox').not('.select-all').change(function() {
    let cl = $(this).val();
    let cls = 'abc' + '' + cl + '';
    if ($(this).is(':checked')) {
        $(this).addClass(cls);
    } else {
        $(this).removeClass(cls);
    }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="checkbox-list">
    <label><input type="checkbox" class="select-all" name="">All</label>
    <label><input type="checkbox" name="" value="1">One</label>
    <label><input type="checkbox" name="" value="2">Two</label>
    <label><input type="checkbox" name="" value="3">Three</label>
    <label><input type="checkbox" name="" value="4">Four</label>
    <label><input type="checkbox" name="" value="5">Five</label>
    <label><input type="checkbox" name="" value="6">Six</label>
    <label><input type="checkbox" name="" value="6">Three</label>
    <label><input type="checkbox" name="" value="7">Four</label>
    <label><input type="checkbox" name="" value="8">Five</label>
    <label><input type="checkbox" name="" value="9">Six</label>
   </div>

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