If a user is using my site between 11pm and 12pm their local time (system time anyway) I need to change a value in an HTML form.
Currently I do this like this:
<a id="dayLink" class="tooltip" name="dayLink" onclick='javascript: document.getElementById("day").value++; window.alert("date has been advanced by 1 day.");'>
This works but it requires that the user see the link, hover over it to read the tool tip, then click the link to change the value.
I would like to automate this so that it happens in the background without the user even knowing.
This is what ive tried:
window.onload=function(){
myMonthFunction();
myDayFunction();
}
function myDayFunction()
{
var d = new Date();
var x = document.getElementById("day");
x.value=d.getDate();
y.value=hours[d.getHours()];
if (y == 11){
document.getElementById("day").value++;
window.alert("date has been advanced by 1 day.");
}
}
window.alert gets called for sure (just there for testing) but the value of day is not incremented. Im not sure why this works as a link but not as a function. Anyone see what im doing wrong?
What worked in the end:
function myDayFunction()
{
var d = new Date();
var x = document.getElementById("day");
x.value=d.getDate();
var hours = d.getHours()
if (hours == 23){
document.getElementById("day").value++;
window.alert("date has been advanced by 1 day.");
}
The variable
yis not defined in the code you posted. So when you attempty.value = ...it won’t execute, since y is undefined. you can correct it by defining y before it is used.Or in your case, since you already have that very element cached in
x, you can use x itself.