0

I have 7 checkbox's for day selection. For example:

<span><input name="days" type="checkbox" value="1" id="day1"><label for="day1">M</label></span>
<span><input name="days" type="checkbox" value="2" id="day2"><label for="day2">T</label></span>
<span><input name="days" type="checkbox" value="3" id="day3"><label for="day3">W</label></span>
<span><input name="days" type="checkbox" value="4" id="day4"><label for="day4">T</label></span>
<span><input name="days" type="checkbox" value="5" id="day5"><label for="day5">F</label></span>
<span><input name="days" type="checkbox" value="6" id="day6"><label for="day6">S</label></span>
<span><input name="days" type="checkbox" value="7" id="day7"><label for="day7">S</label></span>

And I also have an array :

var selectedDays = ["5","6"];

I want the 5th and 6th days to be selected on load. Any short way to do this?

0

5 Answers 5

2
$(document).ready(function () {
  $.each(selectedDays, function(i, val) {
    $('#day'+val).prop('checked', true);
  });
});
Sign up to request clarification or add additional context in comments.

Comments

2
$.each(selectedDays,function(i,v){
   $('input:checkbox[value="'+ v +'"]').prop('checked',true);
});

Comments

1
    $(function () {
        var selectedDays = ["5", "6"];
        $.each(selectedDays, function (i, v) {
            $('input:checkbox[value="' + v + '"]').attr('checked', true);
        });
    });

Comments

1

working Demo http://jsfiddle.net/cse_tushar/3924B/

$(document).ready(function () {
    var selectedDays = ["5", "6"];
    $('input[name="days"]').each(function () {
        for(i=0;i<selectedDays.length;i++){
            if($(this).attr('value') == selectedDays[i]){
                $(this).prop('checked',true);
            }
        }
    });
});

Another Js code

Working Demo http://jsfiddle.net/cse_tushar/3924B/1/

$(document).ready(function () {
    var selectedDays = ["5", "6"];
    $.each(selectedDays, function (i, v) {
        $('input:checkbox[value="' + v + '"]').prop('checked', true);
    });
});

Comments

0

If you want Friday and Saturday be Checked you can do add

checked="checked"

to your input

<input name="days" type="checkbox" value="5" id="day5" checked="checked">
<input name="days" type="checkbox" value="6" id="day6" checked="checked">

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.