Welcome to the Instapage Support Center

|

Redirect to a different page depending on the dropdown option selected

Using custom code you can create almost any functionality on an Instapage landing page.

In the example below I will show you how you can redirect to a URL after form submission depending on the selected option from your form dropdown.

First I will create a dropdown in my form called "My Dropdown", and I will add in three options: "Option 1", "Option 2" and "Option 3".

1__1_.png



I will set it up so that if the visitor selects "Option 1" it will redirect him to our main website https://instapage.com. For "Option 2" we will send him to our Help Center -https://help.instapage.com/hc/en-us and "Option 3" will redirect to this article.

Here you have code that you will need to add to Settings -> JavaScript, Header section:

<script>
document.addEventListener("DOMContentLoaded", function(){
function pageGenerator(fieldName){
return window.__page_generator ? fieldName : base64_encode(fieldName);
}

const dropdownName = "My Dropdown"; //field name
const options = {
"Option 1" : "https://instapage.com", //each option on a new line
"Option 2" : "https://help.instapage.com/hc",
"Option 3" : "https://help.instapage.com/hc/en-us/articles/214133067"
// "Option Name" : "Redirect URL if this option is chosen"
}

const selectInput = document.querySelector('form select[name="' + pageGenerator(dropdownName) + '"]');

window.instapageFormSubmitSuccess = function( form ){
const selectedOption = selectInput.value;
form.querySelector('input[name="redirect"]').value = options[selectedOption];
};

})
</script>

You will need to change the names "Option 1", "Option 2" and "Option 3" with your drop-down fields names and the URLs with the ones you want these fields to redirect to in your case.

The automatic prefilled form feature needs to be removed from the dropdown fields, as it would interfere with the snippet. It can be done by placing the following code under Settings (situated on the right sidebar) - Javascript - Head.

settings-new-EM.png

<script>
$(document).ready(function() {
setTimeout(function() {
$("select").prop('selectedIndex', 0);
}, 2000);
});
</script>

Just keep in mind you also need to set a default redirect to one of the URLs you will add in the code. Click on the form, select Edit, then go to Submission, select Outside URL and enter your URL.

submission.png

outsideurl.jpg

NOTE: It is mandatory to set the default redirect as an outside URL in order for this to work, even if the page is an Instapage page.

You can see a live example below: 

Live Example

In case you need help with a custom functionality or any code customization that is not provided through our Help Center articles we have a Professional Services team that can help our Enterprise users. The Enterprise Solution includes Professional Services as well as additional features and services that can be seen here: https://instapage.com/enterprise-benefits

Have more questions? Submit a request
Was this article helpful?
8 out of 10 found this helpful

Need Further Help?

Tell us what you need and we will be happy to assist you.

Our team of expert support staff and our community can answer your questions. Submit a ticket, or reach out on live chat!