I’m trying to make a form which takes in a users name, and then when the submit button is pressed, an alert is displayed saying good morning, afternoon or evening (depending on time). I feel like I’m in spitting distance but I just can’t get it to work, here is the related snippets of code:
<script type="text/javascript">
<!--
function say_hello() {
var date = new Date();
var time = date.getHours();
var name = document.forms["hello"]["name"].value;
if (time < 12) {
alert ("Good morning, "+name+"!");
}
else if (time > 12 && time < 18) {
alert ("Good afternoon, "+name+"!")
}
else {
alert ("Good evening, "+name+"!")
}
);
}
// -->
</script>
and the HTML:
<form name="hello" action="">
<p>
What is your name?
<input type="text" name="name" size="10">
</input>
</p>
<p>
<input type="button" name="Submit" onclick="say_hello()" value="Receive Personal Message!">
</input>
</p>
</form>
Thank you! 😀
1 Answer