[How to] Conditionally Display Additional Form Fields Based On Dropdown Selection


Userlevel 6
  • Former Unbouncer
  • 198 replies

Want to avoid scaring someone off with too many form fields? Or maybe you want to collect an additional bit of information based on the context of a separate field (state/province based on the country selection, for example). Now you can with our latest addition to Tips and Scripts!

Introducing: Conditional Dropdown Fields :parrot:

This script will allow you to add a drop down field that will conditionally show which field appears next based on what the user chooses.

You can see this in action (built in Unbounce) here:
http://unbouncepages.com/conditional-reveal/

And the State/Province example here:
http://unbouncepages.com/stateprovince-conditional-form-field/


Instructions

Click here to view the instructions

🚨
This is not an official Unbounce feature. This functionality is based entirely on third party code, and has only been tested in limited applications. Since this isn’t a supported Unbounce feature, our support team will be unable to assist if things go awry. So if you are not comfortable with HTML, Javascript and CSS, please consider consulting an experienced developer.


Step 1. Add the Javascript

You can find the javascript here: https://gist.github.com/RobertCam/0be9efb09a091e1880ffeb5edadf2d1a

Since this gets asked for fairly often in Support land, there is also a version of this script designed specifically to conditionally show a state or province field based on the selection of a country: https://gist.github.com/RobertCam/d7ac63b249eba28941872443cb9f2643

Step 2. Create the Form

If you haven’t already, add a dropdown field to the form on your page. The script will target the specified number of fields that follow the dropdown, hiding them and displaying the first field if the first dropdown option is selected, the second field if the second dropdown option is selected, ect…

If you are using the state/province version make sure that the state field comes before the province field in your form.

Step 3. Update the ID of the Dropdown Field, the box containing the form (if there is one), the Submit button, and the “count” Variable

Replace the ID in the script for the for the ID of dropdown field you wish to trigger the conditional logic on your page. Do the same with the submit button and any element the form is nested in. Then adjust the number for the the “count” variable to determine how many fields to conditionally show/hide.

Step 4. Adjust the Spacing of the Following Fields

If the conditional dropdown field is not the last field in your form you may need to slightly adjust the spacing for the fields that follow.

Change the number coming after the “space” variable in the code to adjust the vertical spacing of the following fields. Increasing the number will bring the fields up towards the top of the screen, decreasing it will push them down. It’s going to take a bit of trial and error (sorry).

The default in the script is for 5 fields. To get it working with 2 fields, for example, set the count to ‘2’ and the space to ‘15’.

**V.1 of this script is set to work only with two fields. You can find that here - https://gist.github.com/RobertCam/2e1fee450668037b237a450562fdcae3

You’re Done! :slowparrot:


Can’t see the instructions? Log-in or Join the Community to get access immediately. 🚀


Want to take your Unbounce landing pages + convertables to the next level?
:spinbounce: Check out the Ultimate List of Unbounce Tips, Scripts & Hacks


142 replies

I would like to do the same thing. Did you ever figure this out?

Badge

Hi, I am looking for a similar functionality,

I need a form which when a checkbox is checked it shows an additional phone number field. Is there an existing script for this?

Hello everyone! I have a doubt on how to make this happen:

Basically, I want to:

  • When Dropdown = 0, no fields are shown and the submit button becomes just a link to a specific page.
  • If dropdown = 1 I ask for the email
  • If dropdown = 2, I ask for email and phone

Right now, the scripts I’m using are:

But this is not allowing me to show 2 fields (email and phone) when dropdown is 2.

Thanks

Hey @Carin Did you manage to do this? I’m looking for the same thing

I ended up doing something more “manual” and less pretty, but it works.

Hope somebody can answer my questions so I can make it better.

Userlevel 7
Badge +3

Hi @Alfonsosbla,

You’ll need some custom JS and CSS to get the form working as you want.

The actual JS/CSS would have to be written specifically for your use case since you’ll have to adjust the position of the fields depending on your exact form. The JS would dictate the logic of what fields are shown when and based off of that the CSS would position the actual fields where they need to go.

On top of that you’ll have to play around with the fields validation and turn it on and off depending on what state the form is currently at. I posted a brief snippet above in regards to the validation.

Best,
Hristian

Hi Gary,
Did you ever figure this out?

Hello,

Thank you very much for the script. Im new here, I have no knowledge in doing jscript, I copied the script that was given but I cant seem to make it work with the below conditions, may I ask for help for the script on this one please:

  • When I select US, it should show City, State, Postcode fields.
  • Else, it hides city, state, postcode fields.

Thank you for the help!

Hello,

Thank you very much for the script. Im new here, I have no knowledge in doing jscript, I copied the script that was given but I cant seem to make it work with the below conditions, may I ask for help for the script on this one please:

  • When I select US, it should show City, State, Postcode fields.
  • Else, it hides city, state, postcode fields.

Thank you for the help!

Opps, I just read one reply with the same condition. Im checking the provided solution. Im all good thank yoU!

Hi Rob,

I have implemented conditional form here on a lightbox "http://preference.lookyounger.net/look-younger/"

Now its not letting me submit the form, basically submit btn is not working :(.

Thanks

Badge

Hello ! 

I've just tested this script on my landing page, everything works perfectly, except that it's impossible to submit the form. When you click on the submit button, nothing happens... 
Could you help me? 

Thanks a lot, 
Perrine

I’m also having an issue with this solution. Everything is working except upon submit of form, nothing happens. Any suggestions on what could be wrong?

Userlevel 7
Badge +3

Hi @Perrine & @Timf

It might be a number of reasons… is the console throwing up any errors? 

Can you duplicate your pages on the unboucepages.com domain and share a link here so the community can try and troubleshoot it for you?


 

Badge

Hi @Hristian 

Thanks for your feedback. 

Here's the link to my test page with the form: http://unbouncepages.com/test-atawa/

Thanks for your help! 

Perrine

@Hristian & @Perrine 

 

I figured it out… at least with my use case. I’m using dropdown selectors for conditionally displaying options. Apparently drop-down selectors appear as required by default if the option“first menu choice is not a valid value” is checked. This also applies to conditionally displayed dropdowns causing these fields to be required and causing form submission not to work. Since the form builder doesn’t have a required checkbox to enable/disable for dropdown selectors, I read on another thread to simply uncheck the option “first menu choice is not a valid value.” So apparently this change makes the dropdowns not required. The logic for this kind of makes sense but ideally I’d like to have the main dropdown required and still have instructional text as first option that is not selectable. 

Badge

hello @Timf 

You're right, I've just tested without the "first menu choice is not a valid value" option and my form sends correctly! Like you, it's quite important for me to be able to keep this functionality. 
Do you think there is a solution @Hristian

Thanks :) 

Userlevel 7
Badge +3

Hi @Perrine

It should be possible to adjust the JS and “play” around with what’s required and what not at a particular state - if the field is visible or if it isn’t visible. 

I don’t have a ready made script but based on my experience with these Unbounce forms, it should be possible.  

Reply