Reputation: 3
this is the coding that i am working on and cannot figure out how to have a button just input a basic non-counting timestamp from the current time. Can anyone help me fix my problem please. all i am trying to do is have a time stamp be placed in a box next to the get time button...
<html>
<head>
<script language="JavaScript" type="text/javascript">
function getTimeStamp() {
var now = new Date();
return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':'
+ ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
.getSeconds()) : (now.getSeconds())));
}
window.onclick = "getTimeStamp" ;
</script>
</head>
<body>
<td>
<button type="button" onclick="form"><form name="getTimeStamp">
<input type=text" name="field" value="" size="11">
</form>Get Time</button></td>
<td>Test</td>
</tr>
</body>
</html>
Upvotes: 0
Views: 12029
Reputation: 546
In your code you have some basic errors.
This is working example:
<html>
<head>
<script type="text/javascript">
function getTimeStamp() {
var now = new Date();
return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':'
+ ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
.getSeconds()) : (now.getSeconds())));
}
function setTime() {
document.getElementById('field').value = getTimeStamp();
}
</script>
</head>
<body onload="setTime()">
<input id="field" type="text" name="field" value="" size="11" />
<button type="button" onclick="setTime();">Get Time</button>
</body>
</html>
form
under button
; in this case you can skip the form
input
which you want to set the timeinput
by setting it the IDonload
event in body
element to set the initial timestampIf you have any questions you can ask them.
Upvotes: 0
Reputation: 147483
You can't put a form in a button, the button must be in the form. You need to write the returned value where you can see it.
<form>
<button type="button" onclick="this.form.timeField.value=getTimeStamp()">Get time stamp</button>
<input type="text" name="timeField" size="11">
</form>
Don't give any element in the document a name or ID that is the same as a global variable (e.g. a form and function called "getTimeStamp").
Remove:
window.onclick = "getTimeStamp";
it assigns the string "getTimeStamp" to the onclick property of window, and does nothing useful.
You can also remove:
language="JavaScript" type="text/javascript"
The first was only necessary in very particular circumstances a long time ago, the second was never really necessary other than being required in HTML 4. It's not required any longer. :-)
Upvotes: 1