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



Show first post

142 replies

Userlevel 6

Hi @mclopezz it might be just that you need to add the javascript to the lightbox itself and not the main page. When you open the lightbox in the builder try adding the workaround script to the javascript section there (instead of the main page). Since the lightbox content is in an iframe and javascript on the main page won’t be applied to the lightbox content.

Give that a try and let me know if it fixes the issue for you 🙂

Hi! Thank you so much for this. For some reason the script works when my form is on the page but not when it’s in a lightbox - any way to make it work in a lightbox?

I believe I just need someone to point me in the right direction for an existing article. In a similar vein to this hack, I’d like to add a radio box. I already have a dropdown field where the user selects how many weeks pregnant they are and I can’t change that, so I need an alternative question type to trigger the new fields, is it possible to make this happen with someone selecting a particular radio button option?

So my form is offering a free guide to premature birth testing, and I want to give users the option of ‘would you like to know if your doctor is offering our test?’ if they select ‘yes’ then 2 additional fields would appear. So the same idea as the drop down trigger, but with radio buttons?

Any help would be appreciated!

Does this work with the “Conversational Forms” script ?
https://community.unbounce.com/t/create-conversational-forms-in-unbounce/

I can’t figure it out on my end.

Thanks @Rob! It implemented well.

A note for others who implement this new version: The module.lp.form.data.validation lines in the script are dependent on specific names for the state and province form fields. If you are using field names other than “state” and “province”, you will need to modify 8 lines of JavaScript code to swap out the absolute field names. It’s easy enough to do that, but something you need to be aware of.

Userlevel 6

Hi @voodha unfortunately I haven’t built in this functionality my apologies. It is possible with some adjustments to the code though. Unfortunately I haven’t had the time to make those changes though.

Userlevel 6

HI @Matias_Errecart Glad you found some value here 🙂

This is actually the intended behaviour of the code I’m afraid. The ‘count’ variable determines how many of the fields following the dropdown are hidden. It will then show one field based on the option selected. First dropdown option will show the field immediately after the dropdown, the second dropdown option will show the second field after the dropdown, ect…

Unfortunately I haven’t created the script to show multiple fields. Sorry about that. It would be possible to adapt the script to achieve this functionality though, however this would take a little bit of javascript knowledge.

Userlevel 6

Good catch @kevinridolfi ! Thanks for letting me know! I’ve made some adjustments to the script to allow for each field to be required when visible. It could use a bit of refactoring, but it works. You can see it in action on the example page now: http://unbouncepages.com/stateprovince-conditional-form-field/

One caveat is that the first option in each field will need to be made blank with no text. This will also only work specifically with the state and province fields. Some minor edits are needed to make it work with other fields. Let me know if this does the trick for you.

Here’s the updated script:

<script>
 // NOTE when using a dropdown select for the State and Province fields:
 // If you would like the fields to be 'required' conditionally, the first option must be blank with no text
  
(function(){
  var submitBtn = $("#lp-pom-button-65"); // Add ID of Submit Button
  var field = $("#country");  // Add ID of Dropdown field
  
  var conField1 = field.parent().next();
  var conField2 = conField1.next();
  
  var topPos = conField1.position().top;
  conField2.css({top: topPos});        
  
  conField1.css("display", "none");
  conField2.css("display", "none");
  
  var fieldHeight = conField1.height();
  var space = fieldHeight*2+37;
  var halfSpace = space/2;
  
  var moreFields = $(conField2).nextAll();
  moreFields.animate({top: "-=" + space},0); 
  
  var i = true;
  $(field).change(function() {
     if ($(this).val() == "United States") {
      conField1.fadeIn("slow");
      conField2.fadeOut("slow");
      module.lp.form.data.validationRules.state.required = true;
      module.lp.form.data.validationRules.province.required = false;
      module.lp.form.data.validationMessages.state = "Please select a state"
      if (i) {
        fieldAdjust();
        i = false;
      }
    } else if ($(this).val() == "Canada") {
      conField1.fadeOut("slow");        
      conField2.fadeIn("slow");
      module.lp.form.data.validationRules.province.required = true;
      module.lp.form.data.validationRules.state.required = false;      
      module.lp.form.data.validationMessages.province = "Please select a province"
      if (i) {
        fieldAdjust();
        i = false;
      }
    } else {
      i = true;
      conField1.fadeOut("slow");
      conField2.fadeOut("slow");
      if (conField1.css('display') == 'block' || conField2.css('display') == 'block') { 
        moreFields.animate({top: "-=" + halfSpace}, 600);
        submitBtn.animate({top: "-=" + halfSpace}, 600);
        module.lp.form.data.validationRules.province.required = false;
        module.lp.form.data.validationRules.state.required = false;
      }  
    }
  });
  var fieldAdjust = function() {
    moreFields.animate({top: "+=" + halfSpace}, 600);
    submitBtn.animate({top: "+=" + halfSpace}, 600);
  }
})();  
</script>

Hi,
Your state/province test page has a bug. Both state and province are set to be required. That means the form can’t be submitted. As a test, I selected Afghanistan as my country and I received an error that state is required and province is required (neither of which are obviously showing for Afghanistan).

Do you have a workaround for making either the state or province field required but only when they are visible?

Kevin

Hi Rob! Thanks for this “How to”, it was just what I was looking for!

I have a small problem when i use the code with “var cont”. I paste the code as it is, but it only shows one field per chosen option. Why can this happen?

Currently I only have three example options in the drop-down menu. Is there any other variable that needs to be modified?

Here is my preview page: http://unbouncepages.com/final-asseguras/

I would appreciate your help
Thanks!
Matias.

@Rob,
Is it possible to show more than one field once the user has selected an option on the dropdown field? Script is working fine with one field, but depending on user selection I need to show more fields.

hello All,

I have to create a form where radio button appear based on country selection from drop down list. For example if user select Canada they will see radio button option. If the option is United state than radio button should not be visible.

Can someone help me how to do that.

Userlevel 6

Hi @Sharon_Schanzer I just made a quick edit to variant G for you and it should be good to go 🙂

Hi @Rob checking back in about my question about the bounding box growing and shrinking. Thanks!

@Rob Ah yes, I see that. I was looking at F. What about the bounding box, can we fix it so that shrinks and grows too?

Userlevel 6

I’m previewing variant G where I made the changes and I can see the zip/postal code fine. Are you looking in variant G as well?

Hi @rob http://community.unbounce.com/u/rob Thanks! But now the zip/postal code is permanently hidden. That field should always appear. Thanks! Sharon

Hej Rob,

I’m trying to use your code for a similar but slightly different purpose.
I have a form with a dropdown that consists of different products. The prices of these products differ and I would like a box (or a picture) to appear next to the form depending on which product they choose.
The box/image should then show the price and info regarding that specific product.

I’m kinda a newbie in scripting, so I may be out of my league here.

I hope I explained my case well enough 🙂

Userlevel 6

Ah I see, sorry about that. I just took another look and made some minor adjustments for you so the script only show/hides the one field. I saved the changes in the G variant so you can have a look in Preview mode. The state province field will show if either Canada or the USA is selected 🙂

Hi @rob - yes I saw that version but there’s still one issue which is that in my case I need state & province to be in the same field (we are pushing to pardot and that’s how the form there is set up.) I duplicated what you did and renamed, can you make the 1 field version work? That’s where I got stuck last time. THANKS!

Userlevel 6

just had a look and I can see you are using the default version of the script. I’ve actually created a version specific to your use case, which is linked below the main script at the beginning of the post. I created a new variant for the page and implemented the state/province version of the script for you. I titled the variant “state - province working version” for you (variant F).

Hope this helps!!

Yes, just need state/province, which is required, to appear/disappear for USA/Canada vs being hidden for all other countries. Here’s the URL:

http://unbouncepages.com/all-in-one-robot-software-for-easy-application/

Thanks!

Userlevel 6

Hi @Sharon_Schanzer happy to take a look for you. Could you share the URL of the page? Are you just trying to use the state/province fields in conjunction with the country dropdown when the USA or Canada are selected?

Hello @Rob !

I have tried using the script for state/province and I’m not having any luck. I don’t know JS but even with some trial and error I’m still stuck. Can you help? My need is somewhat urgent.

TIA!

Awesome! Thanks for looking at this for me. I’ll make sure to keep that in mind next time I’m having an issue with preview mode!

Reply