hi

got a bit stuck on this...

function changeDay(newDay){
	ajaxCall('tools/refresh_booking_visual.php','start=' + newDay,
					function(x){
						var pieces = eval("(" + x + ")");
						document.getElementById('booking_visual').innerHTML = pieces.visual;
						document.getElementById('booking_date').innerHTML = pieces.date;
						document.getElementById('date_back').onclick = changeDay(pieces.backdate);
						document.getElementById('date_next').onclick = changeDay(pieces.nextdate);
					}
					);
}

My code is to do with a calendar, when the user clicks the 'Next Day' button it calls the changeDay(), which then uses an ajax call to fetch the calendar details as a json object... shown as pieces.visual, and the new date pieces.date. However in order to call this changeDay function I have been using the onclick event, coded into the html, and now I need to reset the 'back' and 'next' buttons so that they pass the correct unix timestamp to my changeDay()... I tried using

document.getElementById('date_next').onclick = changeDay(pieces.nextdate);

- but the seem to actually call the changeDay() again... i only want it to set it so that when i physically click the next button it fires again...

any ideas... should I rather use

<a href="javascript(changeDay(timestamp)">Next Day</a>

and then

document.getElementById('date_next').href = changeDay(pieces.nextdate);

You're not assigning the function to the onclick the way you're doing it. You're assigning the RETURN of the function to the onclick. You want to return a function from your function so just change your changeDay function to this and use the blah.onclick = changeDay(newDay);

function changeDay(newDay)
{
	return (function (newDay)
	{
		ajaxCall('tools/refresh_booking_visual.php','start=' + newDay, function(x)
		{
			var pieces = eval("(" + x + ")");
			document.getElementById('booking_visual').innerHTML = pieces.visual;
			document.getElementById('booking_date').innerHTML = pieces.date;
			document.getElementById('date_back').onclick = changeDay(pieces.backdate);
			document.getElementById('date_next').onclick = changeDay(pieces.nextdate);
		});
	});
}

man... takes some adjusting to get the hang of all this passing functions backwards and forwards... javascript is like wrestling a snake sometimes...

thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.