# Filling a hidden field with a value

**URL:** <https://community.hubspot.com/t/filling-a-hidden-field-with-a-value/85154>\
**Category:** Lead Capture Tools\
**Tags:** forms\
**Created:** [July 12, 2023, 8:46am UTC](https://community.hubspot.com/t/filling-a-hidden-field-with-a-value/85154 "2023-07-12T08:46:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenrikR](https://avatars.discourse-cdn.com/v4/letter/h/3d9bf3/32.png) [@HenrikR](https://community.hubspot.com/u/HenrikR)\
**Post date:** [July 12, 2023, 8:46am UTC](https://community.hubspot.com/t/filling-a-hidden-field-with-a-value/85154/1 "2023-07-12T08:46:04Z")

</div>

Hey all!  
So what script I should use to fill in a hidden field in a form with a certain value?  
Backstory: we are using global forms for whitepapers and we need to distinguish betweent the submissons to be able to send the right guide.  
This hidden field value will be the differentiator in workflows.

---

<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:** [July 12, 2023, 10:48am UTC](https://community.hubspot.com/t/filling-a-hidden-field-with-a-value/85154/2 "2023-07-12T10:48:30Z")

</div>

Hey @HenrikR there’s no default script to use.

The only supported way to fill hidden fields with a value is via [query strings](https://knowledge.hubspot.com/forms/can-i-auto-populate-form-fields-through-a-query-string). If you just want a static value on a form, then you can easily do this via the forms editor by just setting a default value for the field within the forms editor.

If you want a dynamic value however, there are ways you could capture and set a value via [customising the form](https://legacydocs.hubspot.com/docs/methods/forms/advanced_form_options) embed code too. What code that is though would really depend on what values you’re trying to set. You’ll need a script which determines and sets a value and then passes it to the hidden field.

There’s an article here which is about setting a dynamic redirect for a form, but does have code taking a value from another field. Some of this might help, but you’ll need some JS skills to update it for your needs to write the end code!

[https://community.hubspot.com/t5/APIs-Integrations/Embedded-Form-submission-amp-redirect-URL-based-on-form/m-p/287190](https://community.hubspot.com/t5/APIs-Integrations/Embedded-Form-submission-amp-redirect-URL-based-on-form/m-p/287190)

---

<div class="post-metadata">

**Author:** ![HenrikR](https://avatars.discourse-cdn.com/v4/letter/h/3d9bf3/32.png) [@HenrikR](https://community.hubspot.com/u/HenrikR)\
**Post date:** [July 12, 2023, 11:03am UTC](https://community.hubspot.com/t/filling-a-hidden-field-with-a-value/85154/3 "2023-07-12T11:03:32Z")

</div>

Heyy @TomM2 ! And thanks for the reply 🙂  
I’ll open this up a bit more:

- We would use one global form for all whitepapers
- The global form would have hidden field in it
- The page where the form would be embedded would have a script that would fill in the hidden field with a certain value, which remains the same at all times for that page  
so the script would have the field ID and a value that it fill into the field when the form is submitted. f.ex. we have a Trends ebook -page that has the “Global Whitepaper” form embedded there. The Trends ebook -page would have a script that would fill in the value ‘trends23’ into a field ‘submission\_identifier’. In Hubspot we have a workflow that is triggered with “Global Whitepaper” -form and ‘submission\_identifier’ equals ‘trends23’. Then the workflow would send the right whitepaper email. So the value ‘trends23’ is not dynamic, it is always same for that page. Page query strings are a bit messy, if you have multiple landing pages for the same ebook. Then it is just easier to use the ‘trends23’ identifyer.  
Hopefully this makes sense!

---

<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:** [July 12, 2023, 11:13am UTC](https://community.hubspot.com/t/filling-a-hidden-field-with-a-value/85154/4 "2023-07-12T11:13:01Z")

</div>

Hey @HenrikR that makes sense. Customising the form embed code is your best solution then. You’ll need some JS experience to write this and troubleshoot any issues.

I found a few guides which might be able to help with this though!

[https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/td-p/762173](https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/td-p/762173)

[https://community.hubspot.com/t5/APIs-Integrations/Passing-values-from-js-form-to-hubspot-form/m-p/319943](https://community.hubspot.com/t5/APIs-Integrations/Passing-values-from-js-form-to-hubspot-form/m-p/319943)

[https://community.hubspot.com/t5/CRM/Set-value-of-hidden-form-field-using-Javascript/m-p/485262](https://community.hubspot.com/t5/CRM/Set-value-of-hidden-form-field-using-Javascript/m-p/485262)

A very basic version would just be adding a line to your forms embed code like this:

```auto
hbspt.forms.create({
 css: '',
	portalId: "{{portalId}}",
	formId: "{{formId}}",
	onFormReady: function($form) {
 $('input[name="My_Custom_field"]').val("trends23").change();
 }

```

---

<div class="post-metadata">

**Author:** ![HenrikR](https://avatars.discourse-cdn.com/v4/letter/h/3d9bf3/32.png) [@HenrikR](https://community.hubspot.com/u/HenrikR)\
**Post date:** [July 12, 2023, 12:11pm UTC](https://community.hubspot.com/t/filling-a-hidden-field-with-a-value/85154/5 "2023-07-12T12:11:28Z")

</div>

Super @TomM2 ! thank you 🙂

---

<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:** [July 12, 2023, 12:17pm UTC](https://community.hubspot.com/t/filling-a-hidden-field-with-a-value/85154/6 "2023-07-12T12:17:00Z")

</div>

Happy to help! 🙂

---

<div class="post-metadata">

**Author:** ![js904](https://avatars.discourse-cdn.com/v4/letter/j/97f17d/32.png) [@js904](https://community.hubspot.com/u/js904)\
**Post date:** [November 20, 2023, 8:29pm UTC](https://community.hubspot.com/t/filling-a-hidden-field-with-a-value/85154/7 "2023-11-20T20:29:18Z")

</div>

Wondering if I can tag on here! Also looking to do this but also serve up the relevant content asset via redirect upon form submission. Could the same be done here to have a hidden field to set the redirect just as the ‘submission\_identifier’ example above?

---

<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:** [November 21, 2023, 10:33am UTC](https://community.hubspot.com/t/filling-a-hidden-field-with-a-value/85154/8 "2023-11-21T10:33:31Z")

</div>

Hey @js904 you would want something like this:

[https://community.hubspot.com/t5/APIs-Integrations/Redirecting-Hubspot-form-to-different-urls-depending-on-property/td-p/692081](https://community.hubspot.com/t5/APIs-Integrations/Redirecting-Hubspot-form-to-different-urls-depending-on-property/td-p/692081)
