- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
Embedded Form: Populating form fields
SOLVEAug 8, 2019 2:27 AM
I would like to populate form fields with values via the form embed code. The below code, as recommended, does not work and no errors are generated.
It would be great to get some idea about what's the issue here.
Resources I've checked and followed:
- https://developers.hubspot.com/docs/methods/forms/advanced_form_options
- https://community.hubspot.com/t5/APIs-Integrations/Hubspot-form-programmatically-set-field-value-via...
- https://mikemcbrien.com/hubspot-embedded-form-onformready-onformsubmit/
- https://developers.hubspot.com/manipulating-forms-with-jquery
<!--[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"></script>
<script>
hbspt.forms.create({
portalId: "XXXX",
formId: "XXXX",
onFormSubmit: function($form, ctx){
$('input[name="firstname"]').val('Brian').change();
}
});
</script>
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Accepted Solutions
Aug 20, 2019 3:53 PM
@Kevin-C is correct - your issue is that WordPress loads jQuery in safe-mode which keeps other libraries from using the $ shorthand from colliding the the global namespace.
But - you shouldn't actually need to make a new collection.
The $form argument of the callback is actually a jQuery collection of the entire <form> element.
So instead of:
$('input[name="firstname"]').val('Brian').change();
You can do the following:
$form.find('input[name="firstname"]').val('Brian').change();
That should also be a bit more stable - because there could be a scenario where there are mulitple inputs with the same name on the page - and if that were to happen - $('input[name="firstname"]') would return a collection of more than 1 element which would be problematic.
Hopefully that helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content