How to inject UTC timestamp into a hidden HTML form field from Javascript

I know next to nothing about Javascript. I think I am almost there but need some help…

I need to pass UTC into a hidden field in an HTML form. The actual time doesn’t really matter, just using it as an order identifier that will be unique for each order.

Here is what I have but it isn’t working… Appreciate any help.

— in form section —

<input id="pg_consumerorderid" value="" type="hidden">

— Script just below the < /form> tag

<script>
var field = document.querySelector('#pg_consumerorderid');
var date = Date.now();
// Set the date
field.value = date;
</script>

All my other form fields use input name= instead of the input id= Not sure if that is messing things up.

Thanks!

76 thoughts on “How to inject UTC timestamp into a hidden HTML form field from Javascript”

Leave a Comment