I have a snippet of JavaScript code placed in the head section of my website:
<!-- Form ID Setter -->
<script>
document.querySelector('input[name=form_submission_id]').value = new Date();
</script>
My objective is to automatically insert a date stamp into a hidden form field named 'form submission id', but I'm encountering an error message - Cannot set property 'value' of null.
If I manually reveal the hidden form field using inspector tools, the code functions correctly. However, when the field remains hidden, the error occurs.
You can view the live form here:
I suspect that the issue arises from the form loading after the script execution. Unfortunately, I am unsure how to address this potential delay.
I attempted to implement a timeout delay for the script to run, but this solution did not resolve the problem.
<script>
var formId = function() {
document.querySelector('input[name=form_submission_id]').value = new Date();
}
document.onload = function() {
setTimeout(formId, 3000);
}
</script>