Redirect to Thank You Page Based on Form Select Option (Hubspot)

155 Views Asked by At

I have this code to create a HubSpot form. I am trying to redirect the user to a different page based on what option is selected in the HubSpot field called numemployees.

If the user selects the option of "1-150," the redirect URL should be X. If the user selects any other option, the redirect URL should be Y.

Here is my code. Portal ID, form ID, and redirect URLs are hidden here (fake info used). I used the "external embed" instructions on this page to create this code (I am not well-versed in JS).

<script charset="utf-8" type="text/javascript" src="https://js.hsforms.net/forms/embed/v2.js"></script>
<script>
    hbspt.forms.create({
        region: "na1",
        portalId: "123213213131",
        formId: "123123213213213123123123123121",

        onFormSubmit: function($form) {
            var employeeCount = $form.find('select[name="numemployees"]').val();

            setTimeout( function() {
            if (employeeCount == "1-150" ) {
                window.location.href = "https://www.url1.com";
            }       
            else {
                window.location.href = "https://www.url2.com";
            }      

            }, 500 ); // Waits 1/2 second to redirect.
        }
    });
</script>

While testing, I can only get the redirect for the second URL here, even when I have selected "1-150" as the value for numemployees.

I have a page where this form can be tested if need be, just lmk!

1

There are 1 best solutions below

0
nik218 On

The answer had less to do with my approach and more to do with the fact that HubSpot wasn't using jQuery. With some help from a co-worker, we changed

var employeeCount = $form.find('select[name="numemployees"]').val();

to

const employeeCount = $form.querySelector('select[name="numemployees"]').value;

and that fixed the issue.