I have a dropdown that allows selecting different shipping methods. When selecting one, a different div displays, hiding the other two. I'm using jQuery to smoothly fade the divs in and out. After the animations are done, I need to run the fixHeight method of jQuery DataTables to properly reset the height of the wizard container.
I've tried various methods including queue/dequue but the fixHeight keeps firing too soon. Is there a better way to toggle these divs? And how do I make sure they are complete before executing fixHeight without resorting to adding a callback for each div animation and nesting 4 levels deep?
function toggleShipPickup() {
$("#wizard").smartWizard("hideMessage");
$("#wizard").smartWizard("setError", { stepnum: 2, iserror: false });
$("#wizard").queue(function() {
if ($("#<%= shippingChoice_DropDownList.ClientID %>").val() == "") {
$("#shippingAddressForm").fadeOut();
$("#pickupPhoenix").fadeOut();
$("#pickupTucson").fadeOut();
}
else if ($("#<%= shippingChoice_DropDownList.ClientID %>").val() == "ship") {
$("#shippingAddressForm").fadeIn();
$("#pickupPhoenix").fadeOut();
$("#pickupTucson").fadeOut();
; }
else if ($("#<%= shippingChoice_DropDownList.ClientID %>").val() == "pickupPhx") {
$("#shippingAddressForm").fadeOut();
$("#pickupPhoenix").fadeIn();
$("#pickupTucson").fadeOut();
}
else if ($("#<%= shippingChoice_DropDownList.ClientID %>").val() == "pickupTucson") {
$("#shippingAddressForm").fadeOut();
$("#pickupPhoenix").fadeOut();
$("#pickupTucson").fadeIn();
}
}).dequeue().smartWizard("fixHeight");
}
Edit 1
This version works better, but just feels dirty with all the nesting, but maybe the best way?
function toggleShipPickup() {
$("#wizard").smartWizard("hideMessage");
$("#wizard").smartWizard("setError", { stepnum: 2, iserror: false });
if ($("#<%= shippingChoice_DropDownList.ClientID %>").val() == "") {
$("#shippingAddressForm").slideUp(
function() { $("#pickupPhoenix").slideUp(
function() { $("#pickupTucson").slideUp(
function () { $("#wizard").smartWizard("fixHeight"); });
});
});
}
else if ($("#<%= shippingChoice_DropDownList.ClientID %>").val() == "ship") {
$("#shippingAddressForm").slideDown(
function() { $("#pickupPhoenix").slideUp(
function() { $("#pickupTucson").slideUp(
function () { $("#wizard").smartWizard("fixHeight"); });
});
});
}
else if ($("#<%= shippingChoice_DropDownList.ClientID %>").val() == "pickupPhx") {
$("#shippingAddressForm").slideUp(
function() { $("#pickupPhoenix").slideDown(
function() { $("#pickupTucson").slideUp(
function () { $("#wizard").smartWizard("fixHeight"); });
});
});
}
else if ($("#<%= shippingChoice_DropDownList.ClientID %>").val() == "pickupTucson") {
$("#shippingAddressForm").slideUp(
function() { $("#pickupPhoenix").slideUp(
function() { $("#pickupTucson").slideDown(
function () { $("#wizard").smartWizard("fixHeight"); });
});
});
}
}