I have a js script that produces a calendar and will like to move previous and next. When page loads it checks if month, year is already defined or else use the current month, year. and when the previous button is clicked calculate the previous month and change the month variable. I can get the month and year value to change, but it does not affect the code again. i.e because i have already declared month value at the beginning of the code, if i change the value via an onclick the new value won't affect the code, which i want to. This is the code that is relevant to what i am saying, if you want the full code I will post it. Cheers

cal_current_date = new Date();
var month = returnMonth();
var year = returnYear();

function returnMonth(nMonth){
	var month;
	if(nMonth == undefined){
		month = cal_current_date.getMonth();
		month = nMonth;
	return month;
function returnYear(nYear){
	var year;
	if(nYear == undefined){
		year = cal_current_date.getFullYear();
		year = nYear;
	return year;

function previousMonth(month){
	var pre_month = month -1;
	return pre_month;

var preMonth = previousMonth(month);

var html='<tr><th colspan="7" class="calendar-year"><span class="pre" id="pre">Previous</span>';

$('#pre').click(function() {
	 month = preMonth;


Make sure that cal_current_date is always authoritative. Change its value with eg. .setMonth(), .setDay, .setDate(), .setYear() when next/prev buttons are clicked.

Don't rely on other variables.