# How to pass value with javascript in hidden field

**URL:** <https://community.hubspot.com/t/how-to-pass-value-with-javascript-in-hidden-field/78121>\
**Category:** CMS Development\
**Tags:** javascript\
**Created:** [March 27, 2023, 8:36pm UTC](https://community.hubspot.com/t/how-to-pass-value-with-javascript-in-hidden-field/78121 "2023-03-27T20:36:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PedroCampolina](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@PedroCampolina](https://community.hubspot.com/u/PedroCampolina)\
**Post date:** [March 27, 2023, 8:36pm UTC](https://community.hubspot.com/t/how-to-pass-value-with-javascript-in-hidden-field/78121/1 "2023-03-27T20:36:31Z")

</div>

I need to populate a hidden field in a form that is on a landing page. The form and landing page were created in HubSpot. The hidden field will receive dynamically generated text values that should be saved in a field on the contact, and after submitting the form, this same text should be displayed to the user. I implemented the code below in the page’s footer, but it only works when the field is not hidden. Can anyone support me with a solution?

```auto
<script>
window.addEventListener('message', event => {
 if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormReady') {
 var fm = document.getElementsByName("protocolo_grco");
 	fm.value ="0001";
 console.log (fm.value);
 } 
});

window.addEventListener('message', event => {
 if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {

 	var fm = document.getElementsByName("protocolo_grco");
 	fm.value ="0001";
 	elemento = document.getElementById("output");
 elemento.innerHTML = fm.value;
 }

});
</script>

```

---

<div class="post-metadata">

**Author:** ![TDwebdev](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/tdwebdev/32/78032_2.png) [@TDwebdev](https://community.hubspot.com/u/TDwebdev)\
**Post date:** [March 29, 2023, 7:31am UTC](https://community.hubspot.com/t/how-to-pass-value-with-javascript-in-hidden-field/78121/2 "2023-03-29T07:31:43Z")

</div>

Hi PedroCampolina,

Try this:

```lia-code-sample
<script>
window.addEventListener('message', event => {
 if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormReady') {
 var fm = document.getElementsByName("protocolo_grco")[0];
 fm.value = "0001";
 console.log(fm.value);
 } 
});

window.addEventListener('message', event => {
 if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
 var fm = document.getElementsByName("protocolo_grco")[0];
 fm.value = "0001";
 var elemento = document.getElementById("output");
 elemento.innerHTML = fm.value;
 }
});
</script>

```
