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.
Hey @HenrikR there’s no default script to use.
The only supported way to fill hidden fields with a value is via query strings. 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 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!
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!
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/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:
hbspt.forms.create({
css: '',
portalId: "{{portalId}}",
formId: "{{formId}}",
onFormReady: function($form) {
$('input[name="My_Custom_field"]').val("trends23").change();
}
Super @TomM2 ! thank you ![]()
Happy to help! ![]()
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?
Hey @js904 you would want something like this: