user3672633
user3672633

Reputation: 3

trying to get a timestamp inputed upon a click of the button

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

Answers (2)

Cockootec
Cockootec

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>
  1. you cannot nest form under button; in this case you can skip the form
  2. you need somehow identify the input which you want to set the time
  3. you can reach this input by setting it the ID
  4. in my example I am using onload event in body element to set the initial timestamp

If you have any questions you can ask them.

Upvotes: 0

RobG
RobG

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

Related Questions