500man 500man - 4 months ago 16
HTML Question

automatic time element js

I have just made a from for a warehouse, but I do not feel that the workers should have to fill out the date+time every time, is there any way I can automatically fill a hidden input with the time, using js? I would like the full date+time like this: DD/MM/YYYY H:MM
this is my code:



<html>
<iframe src ="adminview.html" scrolling="no" height ="100"></iframe>
<!--this is the date and time script I am using to try to fill the input-->
<script>
var today = new Date();
var UTCstring = today.toUTCString();
</script>
<form action ="">
Name of employee:<br>
<input type ="text"></input>
Item no.:<br>
<input type ="text"></input>
Quantity of item:<br>
<input type ="text"></input>
<!--this is the input I am trying to fill with the above js-->
<input type ="hidden" value="UTCstring"></input>
</form>
</html>




Answer

Give your hidden input an id (Eg : dateField) and then add this to your script -

document.getElementById('dateField').value = UTCstring;

The above line finds your hidden Date field and sets the value in that. You can now use this value to populate your form with a default one. (If you want to do it on the client side)

Here's the updated code

<html>
<iframe src ="adminview.html" scrolling="no" height ="100"></iframe>
  <!--this is the date and time script I am using to try to fill the input-->
<script>
var today = new Date();
var UTCstring = today.toUTCString();
document.getElementById('dateField').value = UTCstring; // or today.toUTCString();
</script>
<form action ="">
  Name of employee:<br>
<input type ="text"></input>
  Item no.:<br>
<input type ="text"></input>
  Quantity of item:<br>
<input type ="text"></input>
<!--this is the input I am trying to fill with the above js-->
<input id="dateField" type ="hidden"></input>
</form>
</html>