Skip to content

Instantly share code, notes, and snippets.

@RobertCam
Last active November 2, 2017 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertCam/2e1fee450668037b237a450562fdcae3 to your computer and use it in GitHub Desktop.
Save RobertCam/2e1fee450668037b237a450562fdcae3 to your computer and use it in GitHub Desktop.
Conditionally show the following form field based on the selection of a dropdown in an Unbounce form
<script>
/*
Unbounce Community :: Tips & Scripts :: Conditional Form Field Based on Dropdown
TS:0002-04-060
***********************
Do not remove this section. It helps our team track useage of external workarounds.
*/
var submitBtn = $("#lp-pom-button-10"); // Add ID of Submit Button
var field = $("#cats_or_dogs"); // 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.selectedIndex == 1) {
conField1.fadeIn("slow");
conField2.fadeOut("slow");
if (i) {
fieldAdjust();
i = false;
}
} else if (this.selectedIndex == 2) {
conField1.fadeOut("slow");
conField2.fadeIn("slow");
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);
}
}
});
var fieldAdjust = function() {
moreFields.animate({top: "+=" + halfSpace}, 600);
submitBtn.animate({top: "+=" + halfSpace}, 600);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment