dimanche 7 février 2021

How to I set checked value on dynamically created iCheck styled checkbox

I am populating checkboxes dynamically in the ajax success function like this:

//get the json response arrays
var coworker_result = response[0];
var shared_result = JSON.stringify(response[1]);

for (var i in coworker_result)
        {       
            var row = coworker_result[i];
            var coworkerID = row[0];
            var name = row[1];          
            
            //check if the checkbox should be checked or unchecked
            if (shared_result.indexOf(coworkerID) >= 0){
            console.log('found coworker '+coworkerID);
                
            //creating the checkboxes
           $('#icheck-container').append(               
            '<p class="description text-white dd-report-text" ><span style="vertical-align:middle;"> 
            <input type ="checkbox" class="coworker-checkbox" id="'+coworkerID+'"> 
           </span>'+name+'</p>'
            )
            //this is where I attempt to set the checked value, but no love
             $("input#"+coworkerID).iCheck('check');
            }
            else{
            //do something else
            $('input#'+coworkerID+'').iCheck('uncheck');
            )
            
            }
            
        }
    //add iCheck class to the checkboxes
    $('#icheck-container').iCheck({checkboxClass: 'icheckbox_square-grey',radioClass: 'iradio_square- 
   grey'});

Appending the checkboxes works fine, but appending the checked value like this $("#"+coworkerID).iCheck('check'); is the problem. I have browsed a lof of forums and documentation to find the solution, but nothing so fare. If I give the checkbox a static ID it works fine, so I guess the challenge is related to the dynamic ID of the checkboxes.




Aucun commentaire:

Enregistrer un commentaire