Set value of hidden form field using Javascript

I’ve got a small bit of Javascript code in the head of the website:

<!-- Unique form ID -->
<script>
 document.querySelector('input[name="form_submission_id"]').value = new Date();
</script>

My goal is to get a hidden form field with the name ‘form submission id’ to populate with a date stamp, but I keep getting the error -

Cannot set property ‘value’ of null

If I go into inspector tools and unhide the hidden form field the above code works, if it’s hidden I get the error.

Here’s a link to the live form: https://easytrimreveals.com/samples/

Hello @andyrobertson1 , I will add some top experts to this matter, @DanielSanchez @Pixl_Labs any recommendations to @andyrobertson1?

Thank you,

Pam

Any solution for this? I’m trying to get this working ASAP

Ended up getting some help from a friend and it’s solved!

<script>
window.addEventListener('message', event => {
 if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormReady') {
 document.getElementById("hs-form-iframe-0").contentDocument.querySelector('input[name="form_submission_id"]').value = new Date();
 }
});
</script>