# New Forms: How to fill hidden fields with JavaScript

**URL:** <https://community.hubspot.com/t/new-forms-how-to-fill-hidden-fields-with-javascript/127611>\
**Category:** Lead Capture Tools\
**Tags:** forms\
**Created:** [March 9, 2025, 9:02pm UTC](https://community.hubspot.com/t/new-forms-how-to-fill-hidden-fields-with-javascript/127611 "2025-03-09T21:02:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dash-flow-io](https://avatars.discourse-cdn.com/v4/letter/d/6bbea6/32.png) [@dash-flow-io](https://community.hubspot.com/u/dash-flow-io)\
**Post date:** [March 9, 2025, 9:02pm UTC](https://community.hubspot.com/t/new-forms-how-to-fill-hidden-fields-with-javascript/127611/1 "2025-03-09T21:02:00Z")

</div>

Huge pain point:  
The Legacy Forms allowed to inject javascript to for example update form values or write data to hidden fields for tracking or various other use cases.

```lia-code-sample
 <script>
 hbspt.forms.create({
 css: '',
 portalId: '{{portalId}}',
 formId: '{{formId}}',
 onFormReady: function($form) {
 $('input[name="stringtest"]').val("kitkat2").change();
 }
 });
 </script>

```

The new forms use a pure iframe implementation.

```lia-code-sample
<script src="https://js-eu1.hsforms.net/forms/embed/12345.js" defer></script>
<div class="hs-form-frame" data-region="eu1" data-form-id="{{id}}" data-portal-id="{{portal}}"></div>

```

**How can this be done with the new forms?**

With [global form events - set field value](https://developers.hubspot.com/docs/guides/api/marketing/forms/global-form-events#setfieldvalue) one could set hidden fields, but it is unclear how to tell the iframe to execute this javascript.

---

<div class="post-metadata">

**Author:** ![Mike\_Eastwood](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mike_eastwood/32/70075_2.png) [@Mike\_Eastwood](https://community.hubspot.com/u/Mike_Eastwood)\
**Post date:** [March 9, 2025, 10:01pm UTC](https://community.hubspot.com/t/new-forms-how-to-fill-hidden-fields-with-javascript/127611/2 "2025-03-09T22:01:43Z")

</div>

Hi @dash-flow-io

Recently I’ve seen forms int he wild **removing** the iFrame, using a new implementation.

I haven’t been able to figure out when the changes happen.

To target a value in an iFrame this is what I used:

```lia-code-sample
<!--[if lte IE 8]>
 <script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>
<![endif]-->
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js" defer></script>
<script>
var change_me = "123";

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

```

But, as mentioned, I’m looking at changing this and using a new implementation (without the iFrame).

Have fun

Mike

Here to learn more about HubSpot and share my HubSpot Knowledge. I’m the founder of [Webalite](https://www.webalite.com?utm_campaign=community-forum&utm_source=community&utm_content=signature) a Gold HubSpot Partner Agency based in Wellington, New Zealand and the founder of **Portal-iQ** the world’s first automated [HubSpot Portal Audit](https://portal-iq.com?utm_campaign=community-forum&utm_source=community-forum&utm_content=signature) that helps you work smarter with HubSpot.

---

<div class="post-metadata">

**Author:** ![dash-flow-io](https://avatars.discourse-cdn.com/v4/letter/d/6bbea6/32.png) [@dash-flow-io](https://community.hubspot.com/u/dash-flow-io)\
**Post date:** [March 9, 2025, 10:25pm UTC](https://community.hubspot.com/t/new-forms-how-to-fill-hidden-fields-with-javascript/127611/3 "2025-03-09T22:25:12Z")

</div>

the embed code you use seems to be a legacy form. the new forms use different embedding.

---

<div class="post-metadata">

**Author:** ![dash-flow-io](https://avatars.discourse-cdn.com/v4/letter/d/6bbea6/32.png) [@dash-flow-io](https://community.hubspot.com/u/dash-flow-io)\
**Post date:** [March 9, 2025, 10:30pm UTC](https://community.hubspot.com/t/new-forms-how-to-fill-hidden-fields-with-javascript/127611/4 "2025-03-09T22:30:22Z")

</div>

The following approaches also fail:  
1. **FAIL** : prefill values in the iframe div

```lia-code-sample
<div class="hs-form-frame">
data-region="eu1"
data-form-id="{{formid}}"
data-portal-id="{{portal}}"
data-hs-custom-field-myfield="example"
data-hs-prefill="myfield=example"
</div>

```

2. **FAIL** : sending postMessage to iframe

```lia-code-sample
<html>
<script>
document.addEventListener('DOMContentLoaded', () => {
 const FORM_ID = '...';
 const FIELD_NAME = 'myfield'; // Updated field name
 const IFRAME_SELECTOR = `iframe[data-test-id="embedded-form-${FORM_ID}"]`;

 const handleFormReady = (hsIframe) => {
 // Log iframe details
 console.log('HubSpot iframe detected:', hsIframe);
 console.log('Iframe attributes:', Object.fromEntries(
 Array.from(hsIframe.attributes).map(attr => [attr.name, attr.value])
 ));

 // Get iframe origin
 const iframeOrigin = new URL(hsIframe.src).origin;

 // Message handler for HubSpot form events
 const messageHandler = (event) => {
 if (event.origin !== iframeOrigin) return;

 // Debug: Log all messages from HubSpot iframe
 console.log('Received HubSpot message:', event.data);

 if (event.data?.type === 'hsFormCallback' &&
 event.data?.eventName === 'onFormReady' &&
 event.data?.data?.formId === FORM_ID) {

 // Send field update with corrected field name
 hsIframe.contentWindow.postMessage({
 type: 'hsFormRequest', // This is typically correct for HubSpot
 data: {
 fields: [{ 
 name: FIELD_NAME, 
 value: 'example' 
 }]
 }
 }, iframeOrigin);
 }
 };

 window.addEventListener('message', messageHandler);
 };

 // Initial iframe detection
 const hsIframe = document.querySelector(IFRAME_SELECTOR);
 if (hsIframe) return handleFormReady(hsIframe);

 // Setup mutation observer for dynamic loading
 new MutationObserver((_, observer) => {
 const iframe = document.querySelector(IFRAME_SELECTOR);
 if (iframe) {
 handleFormReady(iframe);
 observer.disconnect();
 }
 }).observe(document.body, { childList: true, subtree: true });
});
</script>

<!-- HubSpot embed code -->
<script src="https://js-eu1.hsforms.net/forms/embed/{{someid}}.js" defer></script>
<div class="hs-form-frame" 
 data-region="eu1" 
 data-form-id="{{formid}}" 
 data-portal-id="{{portal}}">
</div>
</html>​

```

---

<div class="post-metadata">

**Author:** ![TomM2](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/tomm2/32/70887_2.png) [@TomM2](https://community.hubspot.com/u/TomM2)\
**Post date:** [March 10, 2025, 10:37am UTC](https://community.hubspot.com/t/new-forms-how-to-fill-hidden-fields-with-javascript/127611/5 "2025-03-10T10:37:15Z")

</div>

Hey @dash-flow-io I can’t definitely see this being a large pain point. I’d recomend sharing this with the product team through a feature request on the [ideas forum here](Https://ideas.hubspot.com)!

---

<div class="post-metadata">

**Author:** ![dash-flow-io](https://avatars.discourse-cdn.com/v4/letter/d/6bbea6/32.png) [@dash-flow-io](https://community.hubspot.com/u/dash-flow-io)\
**Post date:** [March 12, 2025, 3:17pm UTC](https://community.hubspot.com/t/new-forms-how-to-fill-hidden-fields-with-javascript/127611/6 "2025-03-12T15:17:04Z")

</div>

Hi Baskey, i think there is typo, you wrote “can’t” but I think you meant “can”?

---

<div class="post-metadata">

**Author:** ![TomM2](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/tomm2/32/70887_2.png) [@TomM2](https://community.hubspot.com/u/TomM2)\
**Post date:** [March 13, 2025, 4:20pm UTC](https://community.hubspot.com/t/new-forms-how-to-fill-hidden-fields-with-javascript/127611/7 "2025-03-13T16:20:42Z")

</div>

Hi @dash-flow-io yep, you’re correct, that is a typo. I have fat fingers and have to type a lot throughout my day.  
That still doesn’t change my recomendation on opening this as a feature request to the product team however.

---

<div class="post-metadata">

**Author:** ![Mike\_Eastwood](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mike_eastwood/32/70075_2.png) [@Mike\_Eastwood](https://community.hubspot.com/u/Mike_Eastwood)\
**Post date:** [March 30, 2025, 1:34am UTC](https://community.hubspot.com/t/new-forms-how-to-fill-hidden-fields-with-javascript/127611/8 "2025-03-30T01:34:42Z")

</div>

Hi @dash-flow-io

It works for legacy forms! I changed the javascript and removed the iFrame targeting.

**Before** (using old iFrame implementation)

```lia-code-sample
...
 document.getElementById("hs-form-iframe-0").contentDocument.querySelector('input[name="change_me"]').value = change_me;
...

```

**After**

```lia-code-sample
...
 document.querySelector('input[name="change_me"]').value = change_me;
...

```

**Final Code**

```lia-code-sample
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js" defer></script>
<script>
var change_me = "123";

window.addEventListener('message', event => {
 if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormReady') {
 document.querySelector('input[name="change_me"]').value = change_me;
 }
});
</script>

```

Does it work for you?

Mike

Here to learn more about HubSpot and share my HubSpot Knowledge. I’m the founder of [Webalite](https://www.webalite.com?utm_campaign=community-forum&utm_source=community&utm_content=signature) a Gold HubSpot Partner Agency based in Wellington, New Zealand and the founder of **Portal-iQ** the world’s first automated [HubSpot Portal Audit](https://portal-iq.com?utm_campaign=community-forum&utm_source=community-forum&utm_content=signature) that helps you work smarter with HubSpot.

---

<div class="post-metadata">

**Author:** ![dash-flow-io](https://avatars.discourse-cdn.com/v4/letter/d/6bbea6/32.png) [@dash-flow-io](https://community.hubspot.com/u/dash-flow-io)\
**Post date:** [March 30, 2025, 5:06pm UTC](https://community.hubspot.com/t/new-forms-how-to-fill-hidden-fields-with-javascript/127611/9 "2025-03-30T17:06:17Z")

</div>

Your example refers to the legacy forms, not the new multi-step forms.

---

<div class="post-metadata">

**Author:** ![Mike\_Eastwood](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mike_eastwood/32/70075_2.png) [@Mike\_Eastwood](https://community.hubspot.com/u/Mike_Eastwood)\
**Post date:** [March 30, 2025, 3:04am UTC](https://community.hubspot.com/t/new-forms-how-to-fill-hidden-fields-with-javascript/127611/10 "2025-03-30T03:04:09Z")

</div>

Hi @dash-flow-io

p.s. My script is embedded in the html of a custom module; but I’ve also made it work in the javascript using the **onFormReady** [Embed Code Callback](https://developers.hubspot.com/docs/reference/cms/forms#embed-code-callbacks).

**IMPORTANT** : The form must be “Set as raw HTML form” in the Form’s Style Options (so, this only works for Markeitng Pro or Enterprise).

Have fun

Mike

Here to learn more about HubSpot and share my HubSpot Knowledge. I’m the founder of [Webalite](https://www.webalite.com?utm_campaign=community-forum&utm_source=community&utm_content=signature) a Gold HubSpot Partner Agency based in Wellington, New Zealand and the founder of **Portal-iQ** the world’s first automated [HubSpot Portal Audit](https://portal-iq.com?utm_campaign=community-forum&utm_source=community-forum&utm_content=signature) that helps you work smarter with HubSpot.
