Solution 1 :

I’ve created a demo for you, I’ve explained it in the code itself. See if it works for you.

const push_value_of_checker = ['1', '2', '3'];  // get the data in an array-- Your logic

$(document).ready(function() {
  
  //loop through all the checkboxes
  $('.csdtc_chkbox').each(function(key, value_chekbox) {
  
      let $this = $(this);  // current element
      
      // check if the array exists and is not empty
      if (typeof push_value_of_checker !== 'undefined' && push_value_of_checker.length > 0) {

        let value = $(this).val();  // get value of the current checkbox
        //console.log(value);
        let include = push_value_of_checker.includes(value); // check if the value exists in the array
        
        if(include === true){ // if yes, check the checkbox
          $this.prop('checked', 'checked');
        }
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- 
1. All checkboxes should have a name, if you're going to save it.
2. Name is an array so that it can store multiple values.
3. Same name inputs should have different values(here just for demo purpose{value2})
3. Id of all the elements should be unique.
-->
<input type="checkbox" value="1" name="xyz[]" class="csdtc_chkbox" id="any-id-1">Value 1
<input type="checkbox" value="2" name="xyz[]" class="csdtc_chkbox" id="any-id-2">Value 2
<input type="checkbox" value="2" name="xyz[]" class="csdtc_chkbox" id="any-id-3">Value 2
<input type="checkbox" value="3" name="xyz[]" class="csdtc_chkbox" id="any-id-4">Value 3
<input type="checkbox" value="4" name="xyz[]" class="csdtc_chkbox" id="any-id-5">Value 4

Problem :

I currently have problem, I want to prop the checked attribute to the checkbox if the value of two loops is match. so right now I can determine if the value match or not. I will show you guys sample code and illustration where I got already the result if same or not.

Problem: Is there way i can prop the attribute checked to checkbox if the condition is match.

Illustration

Jquery:

var push_value_of_checker = [];

            $(res.children_checker_details).each(function (key_checker, value_checker) {

                push_value_of_checker.push(value_checker.details_id);
            });

            // console.log(push_value_of_checker);

            $('.csdtc_chkbox').each(function (key, value_chekbox) {

                var value = $(this).val();

                $(push_value_of_checker).each(function (key, value_chekboxx) {

                    console.log(value_chekbox);

                    if(value == value_chekboxx) {
                        console.log('match');
                    }
                    else {
                        console.log('not match');
                    }
                });

            });

By

Leave a Reply

Your email address will not be published. Required fields are marked *