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

166
Views
Populate multiple checkbox values by class

How to populate multiple checkbox values to the same class elements fetched from the database with ajax. Checkbox values are comma-separated like this (apple, banana, orange).

$(document).ready(function(){
  var fruits = "banana,apple,orange";
  $(".check").val(fruits);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class"fruits">
<input type="checkbox" class="check" value="apple">
  <label> Apple</label><br>
  <input type="checkbox" class="check" value="orange">
  <label> orange</label><br>
  <input type="checkbox" class="check" value="banana">
  <label> Banana</label><br>
  <input type="checkbox" class="check" value="mango">
  <label> Mango</label><br>
</div>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

$(document).ready(function(){
  var fruits = "banana,apple,orange";
  // Create an array with all the fruits
  var split = fruits.split(',');
  
  // Select each input by the value in this array
  split.forEach(f => {
    // Use the prop checked to check the boxes
    $('input[value="' + f + '"]').prop( "checked", true );
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class"fruits">
<input type="checkbox" class="check" value="apple">
  <label> Apple</label><br>
  <input type="checkbox" class="check" value="orange">
  <label> orange</label><br>
  <input type="checkbox" class="check" value="banana">
  <label> Banana</label><br>
  <input type="checkbox" class="check" value="mango">
  <label> Mango</label><br>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You can use split and includes:

$(document).ready(function () {
            var fruits = ("banana,apple,orange");
            var array = fruits.split(",");

            $('.check').each(function (i) {
                if (fruits.includes($(this).val()))
                   $(this).prop('checked', true);
            });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class"fruits">
<input type="checkbox" class="check" value="apple">
  <label> Apple</label><br>
  <input type="checkbox" class="check" value="orange">
  <label> orange</label><br>
  <input type="checkbox" class="check" value="banana">
  <label> Banana</label><br>
  <input type="checkbox" class="check" value="mango">
  <label> Mango</label><br>
</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!