I’m learning javascript and xhtml for the first time at the moment and I currently have a problem which I have not been able to solve. I think it’s a fairly simple fix, I just don’t have to knowledge to be able to do it. Here’s the javascript and how I’m attempting to call it from the xhtml.
This is in a javascript file called greet.js.
function tOD() {
timeOfDay = new Date
if (timeOfDay.getHours() < 5) {
alert("Either go to bed, or go back to bed " + document.form[0].inputname.value);
}
else if (timeOfDay.getHours() < 11) {
alert("Rise and shine " + document.form[0].inputname.value);
}
else if (timeOfDay.getHours() < 17) {
alert("Good afternoon " document.form[0].inputname.value);
}
else {
alert("And after a hard day at work you can relax at last, " + document.form[0].inputname.value);
}
}
And here is where I’m trying to implement it in my xhtml.
<script type="text/javascript" src="greet.js"></script>
<FORM NAME = input>
Please enter your name:
<input type= "text" name = "inputName"/>
<input Type= Button NAME= "greeting" VALUE="ok" onClick="greet()"/>
</FORM>
The javascript works in that if I take away document.form[0].inputname.value it’ll alert with the correct time of day when I press the button. So it must be the document.form[0].inputname.value, this must be wrong, I’m just unsure how to fix it. I’ve also tried using elements[0] instead of form[0] and also input and element[input] so I’m not sure what it could be…
Thanks, all help is appreciated.
You have some errors, I have made a working version for you. Please read it slowly and try to see your errors and fix them 😉
You can see it here: http://jsfiddle.net/3U5Qm/