You have to keep a variable indicating the current week that your list is showing.
Every hit on the next
/previous
buttons should increment/decrement that variable accrodingly.
The variable could contain a Date
object referencing the first day of week. i.e.
function getCurrentWeek () {
var dt = new Date();
if (dt.getDay() !== 0) {
dt.setDate(dt.getDate() - dt.getDay());
}
return dt;
}
function incrementWeeks (week, howMany) {
var next = new Date(week); // Copy it, to not modify original variable
next.setDate(next.getDate() + 7 * (howMany == undefined ? 1 : howMany));
return next;
}
Now eveytime you hit the button to show the list of days - just generate the days dynamically based on the current week.
This could look something like this:
var currentWeek = getCurrentWeek();
var dayStrings = ['su', 'ma', 'ti', 'ke', 'to', 'pe', 'la'];
document.getElementById('next-button').addEventListener('click', function () {
currentWeek = incrementWeeks(currentWeek, 1);
});
document.getElementById('previous-button').addEventListener('click', function () {
currentWeek = incrementWeeks(currentWeek, -1);
});
document.getElementById('show-week-button').addEventListener('click', function () {
var select = document.getElementById('show-week-select');
select.innerHTML = '';
for (var dt = currentWeek, next = incrementWeeks(dt, 1);
+dt < +next;
dt.setDate(dt.getDate() + 1)) {
var option = document.createElement('option');
option.text = dayStrings[dt.getDay()] + ' ' + dt.getDate() + '.' (dt.getMonth() + 1) + '.' + dt.getFullYear();
option.value = +dt;
select.options.add(option);
}
// Show the SELECT
// `click()` may not work, so you can try this (http://stackoverflow.com/questions/249192/how-can-you-programmatically-tell-an-html-select-to-drop-down-for-example-due)
// or use a js-based dropdown
// or just listen to when the select element opens instead of a simple click
select.click();
});
This code is untested. I hope you get the idea.