Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!