nionta's avatar

Loop between append

I want to checked a check box if it fill up a condition, I want to check the condition into a append function in my jQuery code. here is my code

if(index=='branchprograms')
                          {
                             branchObj.forEach(function(element) {
                             
                              
                              arr = element.branches_program_id;
                             });
                          }

    else
                          {
                              console.log(arr);
                              branchObj.forEach(function(element) {
                              $('#progvalue').append(
                                  '<tr><td>'+'<input type="checkbox" name="program_multi_id[]"          value="'+element.program_id+'" for(var i = 0; i < arr.length;i++){ if(element.program_id == arr[i]) checked }>'+element.program_name+'</td></tr>');
                             });
                          } 

but in else condition neither the for loop nor the if condition is working. can anyone help, what the code should be?

0 likes
3 replies
ardnor's avatar
ardnor
Best Answer
Level 6

@nionta Maybe this code will help to your problem.

branchObj.forEach(function(element) {
    var isChecked = '';

    if(arr.includes(element.program_id)) {
    isChecked = 'checked';
   }

  var html = '<tr>';
  html += '<td>';
  html += '<input type="checkbox" name="program_multi_id[]" value="'+element.program_id+'" '+isChecked+'>';
  html += '</td>';
  html += '</tr>';
});
nionta's avatar

@lancecoder though your answer is not the direct solution, but it helped me a lot, thank you

Please or to participate in this conversation.