Html Code:
<div id="weekdays">
<label for="saturday"> Saturday </label><input type='checkbox' name='week_day[]' value='saturday' id="saturday">
<label for="sunday"> Sunday </label><input type='checkbox' name='week_day[]' value='sunday' id="sunday">
<label for="monday"> Monday </label><input type='checkbox' name='week_day[]' value='monday' id="monday">
<label for="tuesday"> Tuesday </label><input type='checkbox' name='week_day[]' value='tuesday' id="tuesday">
<label for="wednesday"> Wednesday </label><input type='checkbox' name='week_day[]' value='wednesday' id="wednesday">
<label for="thursday"> Thursday </label><input type='checkbox' name='week_day[]' value='thursday' id="thursday">
<label for="friday"> Friday </label><input type='checkbox' name='week_day[]' value='friday' id="friday">
Array
var f = ["saturday", "sunday", "monday"]
info I wanna check the days saturday, sunday and monday on this form
A jQuery selector is nothing but a string, so just join the array into a string of ID's :
$('#'+f.join(', #')).prop('checked', true);
FIDDLE
With the forEach method of Array this is very simple even without JQuery.
f.forEach(function(i){
document.getElementById(i).checked = true;
});
You can read more about for each on the Mozilla Developer Network
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With