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



Show first post

142 replies

Hey @Amy_Sera,

Have you considered this script instead? It allows you to start with the easy questions and work your way up to the meaty questions. If you give this a try I’d love to hear how it works out for you!

Awesome! I’ll check it out and let you know. While I have you, do you have any tips for hiding a box based on the time of day? Like if I have a floating phone number click to call box, but my call center closes at a certain time, it would be nice to hide that box after 6pm MST for example.

Ahhhhhh, very interesting use case. I’m sure this would be possible with a bit of code. @Vic / @Rob / @Noah - you know of anything off the top of your head?

Userlevel 3

Hi Amy!

I do have some code that can help you hide any element on your landing page between two hours.

Simply paste this code in the javascripts section of your page selecting ‘Head’ as the placement:

<script>
$(document).ready(function(){
    
   //Add the start and end time using the format hh:mm (24 hour format) Time is in UTC. 
	var startTime = '13:00';
	var endTime = '21:00';

	var curr_time = getval();

	if (curr_time > startTime && curr_time < endTime) {
    }else{
   
   //Change #lp-pom-box-15 for the ID of the element you want to hide.    
  		$('#lp-pom-box-15').hide();
    }
  
	function getval() {
    	var currentTime = new Date()
    	var hours = currentTime.getUTCHours()
    	var minutes = currentTime.getUTCMinutes()
    	if (minutes < 10) minutes = "0" + minutes;
    	var current_time = hours + ":" + minutes;
    	return current_time;
	}  
});
</script>

Now, change the ‘startTime’ and the ‘endTime’. This is the period of time the element will be visible on the page. Keep in mind this times are in UTC, to make sure it’s the same time all around the world. There are many resources on line to help you find your UTC time, here’s one of them.

Don’t forget to change #lp-pom-box-15 for the ID of your own box or element, save and republish.

That’s it! Please give it a try and feel free to reach out if you have any other questions. Keep in mind we are not always able to support this type of unofficial workarounds, but we are always happy to take a look and at least try to point you in the right direction 🙂

Have a great day, Amy!

Awesome, @Vic! Let’s build this into an official Tip & Script that we can add to the library. 🙂

Thank you both so much, this is awesome! If it ever makes it into a feature it would be good to have the flexibility to combine day of week. So hide it monday for these hours, tuesday for those hours, etc.

@Rob Hi Rob, I’m trying to combine this code with your “Dynamic form confirmation urls using browser redirects” code. I’m trying to have the phone number/comment section display conditionally based on what they’re looking for. I’ve got both working, however there are some issues.

  1. If I fill out any of the fields it won’t redirect to the designated url.
  2. If I choose more than one dropdown option (without filling any other fields) it doesn’t redirect to the final choice’s designated url (or any url for that matter).
    If I don’t fill out any fields and only select one dropdown option it works like I want it to (I just don’t get any information aside from knowing what they’re looking for).

Any idea of what’s causing this issue and a possible solution? Or is what I’m trying to achieve not even possible? Any help is greatly appreciated!
Thanks, James

I’ve implemented the github code on this page, http://test.pretrm.com/pretrm-test-guide/, the idea is to take people who answer ‘21 weeks or over’ for field ID im_pregnant to pretrm.com as a test but it’s not working. It still sends people to the default URL from the click action on the form?

@Rob - I was able to get it to work, but only by reducing spaces in the drop down. So if ‘21 weeks or more’ is an option the code doesn’t work, however, ‘21+’ works. It would be nice if the code was capable of handling menu options with spaces. If that is not possible I’m okay with the workaround I have.

Userlevel 6

Hey @JamesNhappy to take a look for you, could you share the URL of your page? To be honest the Dynamic Form Confirmation workaround I posted a while back isn’t my favourite (I find it to be overly complex and there are a lot of pitfalls when implementing it). This little script from Noah is a much nicer way of doing it in my opinion: https://gist.github.com/noahub/ee4fd2a1eb8b3096cf29952548ebedab

Let me know on that URL though and I’ll take a look for ya 🙂

Hey @Rob, I got the script from Noah to work. I like how yours still allowed the Form Confirmation dialog to show though since I don’t want all all my selections to have a page redirect and would like if possible to use that code (unless you know how to get a FCD with Noah’s code). I don’t have the page published as I created a test page with just the form on it, so I don’t know if that’ll matter with what you need but if it does please let me know and I can publish it. The preview url for the page with your codes is https://app.unbounce.com/1634653/pages/8136cf10-907e-4a78-a1ee-ad8b8af4052b/preview?variant_id=140740896#desktop Please let me know if you have any ideas. Thanks, James

Hello @Rob,

I was able to get the Country/Province script to work by itself but if I wanted to add another field option like an additional dropdown for Australia State/Territory, how do I make it work? I have tried adding a conField3 to cover this option and now none of options are working.

Thank you.

Userlevel 6

Sorry for the late reply on this James! I’ve had a look and I don’t see any obvious reason why the code isn’t working on the page. Would it be ok with you if I published the test page to an unbouncepages.com URL so I can test it fully? The redirect won’t occur in Preview.

Userlevel 6

Hey @AlanJ the state/province version is meant to work specifically with the default state province fields for a form. You could try using the original version of the script here: https://gist.github.com/RobertCam/0be9efb09a091e1880ffeb5edadf2d1a

Happy to take a closer look for you if you share the URL for the page 🙂

Hello @Rob,

Just messaged you. 💬

Thank you for the help.

Hey @Rob. Yes that’s fine if you publish it. Just a note, in the preview mode I can only get a page redirect if I don’t click on any of the other fields. As soon as I click on a field other than the drop down (regardless of whether I type something in or not) the page no longer redirects.

Userlevel 6

Hi James,

I just published the page and everything is working correctly. I think it was just Preview mode that was causing the issues. Most external links don’t function properly in Preview as it’s actually an iframe of the page (since it’s technically unpublished). As soon as I published it everything was working fine. I didn’t even touch the code 🙂

Nice work!

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!

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!

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?

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

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!!

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

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 🙂

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 🙂

Reply