0

I am trying to auto fill a form element with the current date and time in a specific format required by mySQL. My difficulty is auto-filling this value into a form element. So far I have tried using .getElementById("element_id").innherHTML = timestamp But this does not result in any text filling the form element:

<html>
<head>
<title>Create New Reservation</title>
<script>
function Timestamp()
{
const currentTime = new Date();
var year = currentTime.getFullYear();
var month = currentTime.getMonth();
var day = currentTime.getDate();
var dash = "-";
var space = " ";
var colon = ":";
var hour = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var timestamp = year.toString().concat(dash, month, dash, day, space, hour, colon, minutes, colon, seconds);
document.getElementById("occurred").innerHTML = timestamp;
}
</script>
</head>
<body onload="Timestamp()">
<h1>Enter Reservation Details</h1>
<form method="POST" action="reservation_process.php">
<fieldset>
<label>Current Time</label><type="text"/>
<input type="text" id="occurred" name="occurred"
<br>
<input type="submit" name="Process" value="Create Entry" />
</form>
</body>
</html>

I have also tried returning the formatted timestamp from the function directly into the value="" attribute, but I suspect my syntax is incorrect and I have not been able to find any HTML documentation on how to do this properly:

<input type="text" id="occurred" name="occurred" value= Timestamp() >

1 Answer 1

1

You can use $('#occurred').val(timestamp); instead of document.getElementById("occurred").innerHTML = timestamp; inside your function.

$(element).val(value) sets the value of an input, if you not already have you need to include jQuery to use this.

function Timestamp(){
  const currentTime = new Date();
  var year = currentTime.getFullYear();
  var month = currentTime.getMonth();
  var day = currentTime.getDate();
  var dash = "-";
  var space = " ";
  var colon = ":";
  var hour = currentTime.getHours();
  var minutes = currentTime.getMinutes();
  var seconds = currentTime.getSeconds();
  var timestamp = year.toString().concat(dash, month, dash, day, space, hour, colon, minutes, colon, seconds);
  $('#occurred').val(timestamp);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head>
<title>Create New Reservation</title>
<script>

</script>
</head>
<body onload="Timestamp()">
<h1>Enter Reservation Details</h1>
<form method="POST" action="reservation_process.php">
<fieldset>
<label>Current Time</label><type="text"/>
<input type="text" id="occurred" name="occurred"
<br>
<input type="submit" name="Process" value="Create Entry" />
</form>
</body>
</html>

Sign up to request clarification or add additional context in comments.

1 Comment

Why jQuery??? OP just need to change document.getElementById("occurred").innerHTML to document.getElementById("occurred").value!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.