0
var ds = $("#recDay").val().split(/ +/); 
var weekArray = [];
for (var i=0; i<ds.length; i++){
    weekArray = ds[i];
} // i =0 , weekArray = Sunday; i=1, weekArray = tuesday; i=2, weekArray = Wednesday

<table>
<tr>
<td width="3%"></td>
<td><input type="checkbox" name="weeklyDay" id="sunday" value="sunday"> Sunday</td>      
<td><input type="checkbox" name="Day" id="monday" value="monday"> Monday</td>                                                                               
<td><input type="checkbox" name="Day" id="tuesday" value="tuesday"> Tuesday</td>                                                                                
<td><input type="checkbox" name="Day" id="wednesday" value="wednesday">Wednesday</td>                                                                              
</tr>                                                                            
<tr>                                                                                
<td width="3%"></td>                                                                                
<td><input type="checkbox" name="Day" id="thursday" value="thursday" > Thursday</td>                                                                                   
<td><input type="checkbox" name="Day" id="friday" value="friday"> Friday</td>                                                                                 
<td><input type="checkbox" name="Day" id="saturday" value="saturday"> Saturday</td>                                                                         
</tr>                                                                       
</table>

How to check only the check boxes that is in weekArray? The code which I tried is

$('select[name="Day"] option').filter(function(){
         if(weekArray.indexOf((this).text(), 0) != -1);
         return $(this);
    }).prop("checked", true);

But this is not working. Any help would be appreciated

4 Answers 4

1

You can do it this way,

Live Demo

var weekArray = ["saturday","monday","tuesday","friday" ];
$('.weekdays').filter(function () {    
    if (weekArray.indexOf(this.id) != -1)
          return $(this).closest('td').find(':checkbox');
}).prop("checked", true);
Sign up to request clarification or add additional context in comments.

Comments

1

your are using select selector for checkbox ????..

 $('#divID input:checkbox').each(function(){
     if($.inArray($(this).val(), weekArray) != -1){
       $(this).prop("checked", true);
    }
});

2 Comments

my checkboxes are inside div. Should I make any changes for it ?
yes you can ... since $(input:checkbox) find all checkbox in the document.. to be precise you can add the divs id to it..$(#divID input:checkbox)
0
if(in_array(document.getElementById("CheckBoxId"), $yourArray)) { CheckBox.Checked }

Comments

0
for (var i=0; i<ds.length; i++){
    weekArray = ds[i];
}

$("form input:checkbox").each(function(){
if(jQuery.inArray($(this).attr('id'), weekArray )!=-1)
$(this).prop('checked', true);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.