Customize Form Embed for Redirect Based on Checkbox Click


#1

Trying to figure out how to customize this form embed so that if the checkbox is selected, the user is redirected to Thank You Page #1 on form submit. If unchecked, the user is redirected to Thank You Page #2.

Struggling on manipulating the embed code, but this is what I have so far (my logic behind it, if nothing else).

I know the whole “redirect” variable isn’t correct code, but how would I do this? Thanks in advance!

<script>
            hbspt.forms.create({
                css: '',
                portalId: 'HUB ID HERE',
                formId: 'GUID-GOES-HERE',
                redirectUrl: {{redirect}},
                onFormReady:  function ($form) {
                    var box = $("#preferred_agent_toolkit_checkbox-GUID-GOES-HERE").change(function() {
                        if($('#preferred_agent_toolkit_checkbox-GUID-GOES-HERE').prop('checked')) {
                            alert('checked');
                            {% set redirect = 'http://google.com' %}
                        } else {
                            {% set redirect = 'http://yahoo.com' %}
                        }
                    }
                },
                onFormSubmit: function ($form) {
                }
            });

        </script>

#2

An update to trying to figure this out. Still could use some help.
I’m attempting to replace the name of the input with the redirect values I want. The following still redirects me to the default redirectUrl despite checking the checkbox BUT my console.logs reflect the replacement is working. Any ideas?

        hbspt.forms.create({
        css: '',
        portalId: '259915',
        formId: 'my_guid',
        redirectUrl: 'http://google.com',
        onFormReady: function ($form) {
            $("input[name=preferred_agent_toolkit_checkbox]").change(function () {
                if($("input[name=preferred_agent_toolkit_checkbox]").prop('checked')) {
                    var new_hs_context = $("input[name=hs_context]").val();
                    console.log('checked', new_hs_context);
                    new_hs_context = new_hs_context.replace("http://google.com", "http://www.yahoo.com");
                    new_hs_context = new_hs_context.replace("http://google.com", "http://www.yahoo.com");
                    console.log('checked after replace', new_hs_context);
                    $("input[name=hs_context]").val(new_hs_context);
                } else {
                    var new_hs_context = $("input[name=hs_context]").val();
                    console.log('not checked', new_hs_context);
                    new_hs_context = new_hs_context.replace("http://google.com", "http://www.cnn.com");
                    new_hs_context = new_hs_context.replace("http://google.com", "http://www.cnn.com");

                    console.log('not checked after replace', new_hs_context);
                    $("input[name=hs_context]").val(new_hs_context);
                }
            });
        }
    });