Reputation: 621
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
Upvotes: 0
Views: 1978
Reputation: 36531
your are using select
selector for checkbox
????..
$('#divID input:checkbox').each(function(){
if($.inArray($(this).val(), weekArray) != -1){
$(this).prop("checked", true);
}
});
Upvotes: 1
Reputation: 148110
You can do it this way,
var weekArray = ["saturday","monday","tuesday","friday" ];
$('.weekdays').filter(function () {
if (weekArray.indexOf(this.id) != -1)
return $(this).closest('td').find(':checkbox');
}).prop("checked", true);
Upvotes: 1
Reputation: 8871
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);
});
Upvotes: 0
Reputation: 1384
if(in_array(document.getElementById("CheckBoxId"), $yourArray)) { CheckBox.Checked }
Upvotes: 0