# Set value of hidden form field using Javascript

**URL:** <https://community.hubspot.com/t/set-value-of-hidden-form-field-using-javascript/42901>\
**Category:** CRM\
**Tags:** forms\
**Created:** [August 25, 2021, 8:05pm UTC](https://community.hubspot.com/t/set-value-of-hidden-form-field-using-javascript/42901 "2021-08-25T20:05:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andyrobertson1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/andyrobertson1/32/35608_2.png) [@andyrobertson1](https://community.hubspot.com/u/andyrobertson1)\
**Post date:** [August 25, 2021, 8:05pm UTC](https://community.hubspot.com/t/set-value-of-hidden-form-field-using-javascript/42901/1 "2021-08-25T20:05:21Z")

</div>

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

```lia-code-sample
<!-- 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/](https://easytrimreveals.com/samples/)

---

<div class="post-metadata">

**Author:** ![PamCotton](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/pamcotton/32/47916_2.png) [@PamCotton](https://community.hubspot.com/u/PamCotton)\
**Post date:** [August 26, 2021, 10:06pm UTC](https://community.hubspot.com/t/set-value-of-hidden-form-field-using-javascript/42901/2 "2021-08-26T22:06:19Z")

</div>

Hello @andyrobertson1 , I will add some top experts to this matter, @DanielSanchez [@Pixl\_Labs](https://community.hubspot.com/t5/user/viewprofilepage/user-id/156228) any recommendations to @andyrobertson1?

Thank you,

Pam

---

<div class="post-metadata">

**Author:** ![andyrobertson1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/andyrobertson1/32/35608_2.png) [@andyrobertson1](https://community.hubspot.com/u/andyrobertson1)\
**Post date:** [August 31, 2021, 4:40pm UTC](https://community.hubspot.com/t/set-value-of-hidden-form-field-using-javascript/42901/3 "2021-08-31T16:40:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andyrobertson1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/andyrobertson1/32/35608_2.png) [@andyrobertson1](https://community.hubspot.com/u/andyrobertson1)\
**Post date:** [September 1, 2021, 3:04pm UTC](https://community.hubspot.com/t/set-value-of-hidden-form-field-using-javascript/42901/4 "2021-09-01T15:04:55Z")

</div>

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

```lia-code-sample
<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>

```
