I was trying to do something similar. A vendor gave me the code at the front of this thread but it did not work. The browser did not like the “onFormSubmit” event.
I found this comment in another thread https://community.hubspot.com/t5/APIs-Integrations/Include-form-fields-as-redirect-URL-parameters-on-an-embedded/m-p/431131/highlight/true#M42820
I used that to create code below. It removes the logic from the JS that writes the form on the page, thus eliminating the “onFormSubmit” issue. I also replaced “$” with “jQuery” to avoid JS or jquery version conflicts (it’s a WordPress site where multiple versions of jQuery are loaded). Thankfully, this worked. Problem solved for me.
Code:
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/embed/v2.js"></script>
<script>
hbspt.forms.create({
region: "na1",
portalId: "20883767",
formId: "373234b2-7f83-43f9-96f1-0180bc458223"
});
</script>
<script>
jQuery(document).ready(function() {
setTimeout(function() {
var form = jQuery('[data-form-id=373234b2-7f83-43f9-96f1-0180bc458223]');
form.submit(function() {
var choice = jQuery('[data-form-id=373234b2-7f83-43f9-96f1-0180bc458223]').find('select[name="how_many_gas_electric_hybrid_vehicles_currently_managed_"]').val();
setTimeout(function() {
if (choice == '1-2'){
window.location.href = 'https://springfreeev.com/thank-you-b';
} else if (choice == '3-10'){
window.location.href = 'https://springfreeev.com/thank-you-b';
} else if (choice == '11-24'){
window.location.href = 'https://springfreeev.com/thank-you-a';
} else if (choice == '25+'){
window.location.href = 'https://springfreeev.com/thank-you-a';
}
}, 500);
});
}, 500);
});
</script>