Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda