I have a duration of for example 3 days, and 7 checkboxes that represent each day in a week. I want to be able to check one day, and then hide the next 2 days (3 days total).
If we consider a duration of 3 days: I check Monday, then tuesday & wednesday will be hidden. I can then check thursday, then friday & saturday will be hidden:
I wrote the following that partially works:
var toHide = new Array(0,0,0,0,0,0,0,0);
$('.days').change(function() {
var val = $(this).val();
var duration = $('#duration').val();
if($('#check'+val).prop('checked')) {
scheduleSelection(val,duration,'on');
}else{
scheduleSelection(val,duration,'off');
}
});
function scheduleSelection(id,duration,mode) {
var totalDays = duration;
if(totalDays>1) {
totalDays--;
var next = parseInt(id)+1;
for(i=1;i<=totalDays;i++) {
if(next>7) { next = 1; }
if(mode=='on') {
toHide[next] = 1;
$('#check'+next).hide();
}else{
toHide[next] = 0;
$('#check'+next).show();
}
next++;
}
}
}
The problem here is if you were to check Monday then tuesday & wednesday will be hidden, but if you then check saturday, there is a collision as sunday & monday will be hidden, yet tuesday & wednesday will remain hidden:
Please see jsFiddle
Any pointers in the right direction is appreciated
Sun
?