CRM

andyrobertson1
Contributor

Set value of hidden form field using Javascript

SOLVE

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/

1 Accepted solution
andyrobertson1
Solution
Contributor

Set value of hidden form field using Javascript

SOLVE

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>

 

View solution in original post

3 Replies 3
andyrobertson1
Solution
Contributor

Set value of hidden form field using Javascript

SOLVE

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>

 

PamCotton
HubSpot Alumni
HubSpot Alumni

Set value of hidden form field using Javascript

SOLVE

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

 

Thank you,

Pam

andyrobertson1
Contributor

Set value of hidden form field using Javascript

SOLVE

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

0 Upvotes