jeudi 15 janvier 2015

Javascript set checkbox checked between selected values

Following the code



<select id="choice_option">
<option value="00">Hourly</option>
<option value="15">15 minutes</option>
<option value="30">30 minutes</option>
<option value="45">45 minutes</option>
</select>

<select id="from_time">
<option value="6:00">6:00</option>
<option value="7:00">7:00</option>
<option value="8:00">8:00</option>
...
</select>

<select id="to_time">
<option value="6:00">6:00</option>
<option value="7:00">7:00</option>
<option value="8:00">8:00</option>
...
</select>

<input type="checkbox" value="6:00" class="times" />6:00
<input type="checkbox" value="6:00" class="times" />6:15
<input type="checkbox" value="6:00" class="times" />6:30
<input type="checkbox" value="6:00" class="times" />6:45
<input type="checkbox" value="6:00" class="times" />7:00
<input type="checkbox" value="6:00" class="times" />7:15
<input type="checkbox" value="6:00" class="times" />7:30
<input type="checkbox" value="6:00" class="times" />7:45
...
var choice = document.getElementById('choice_option');

Event.add('choice_option', 'change', function() {
checkTimes(choice);
});

var checkTimes = function(elem) {
var from = document.getElementById('from_time'),
to = document.getElementById('to_time'),
times = document.querySelectorAll('.times'),
len = times.length,
i, t, startTime, stopTime;

for(i = 0; i < len; i++) {
t = times[i].value.slice(-2);
startTime = from.options[from.selectedIndex].value;
stopTime = to.options[to.selectedIndex].value;

if(times[i].checked) {
times[i].checked = false;
}

if(elem.options[elem.selectedIndex].value == t) {
times[i].checked = true;
}
}
}


What I want to achieve is set the checkboxes as checked between users's selected hours "from_time" and "to_time".


I made it till the point where checkboxes get checked based on :00, :15, :30 or 45 minutes. But I'm stuck when the checked boxes should be between selected times.


So if user chooses 15 minutes and from 06:00 to 14:00 I want every checkbox which has the value 6:15, 7:15, 8:15 ... 14:15 to be checked.


I tried to compare the "from_time" and "to_time" but no luck so far.


Thank you





Aucun commentaire:

Enregistrer un commentaire