0

I built an order form on a site and used some js to navigate through the order process. After one step is finished the user should click on the "next" button and then various div should show/hide:

$("#buttonShowStep2").click(function() {
    $(this).hide("blind", 200);
    $("#outerDivStep2").show("blind", 300, function() {
        $('html, body').animate({ scrollTop: $(".scrollToHeading2").offset().top - 20 });
        document.getElementById("checkHeading1").className = "orderChecked";
    });
});

$("#buttonShowStep3").click(function() {
    $(this).hide("blind", 200);
    $("#outerDivStep3").show("blind", 300, function() {
        $('html, body').animate({ scrollTop: $(".scrollToHeading3").offset().top - 20 });
        document.getElementById("checkHeading2").className = "orderChecked";
    });
});

$("#buttonShowStep4").click(function() {
    $(this).hide("blind", 200);
    $("#outerDivStep4").show("blind", 300, function() {
        $('html, body').animate({ scrollTop: $(".scrollToHeading4").offset().top - 20 });
        document.getElementById("checkHeading3").className = "orderChecked";
    });
});

As you can see the code is very... long. I tried to built a for loop around it to wrap it a little bit. Unfortunately it is not working.

Question: Is it possible to built a for loop with multiple nested functions? Thanks for your help!

for (var i = 2; i <= 4; i++) {
    var buttonShowStep = "#buttonShowStep" + i;
    var outerDivStep = "#outerDivStep" + i;
    var scrollToHeading = ".scrollToHeading" + i;
    var checkHeading = "#checkHeading" + i -1;
        $(buttonShowStep).click(function() {
            $(this).hide("blind", 200);
            $(outerDivStep).show("blind", 300, function() {
                $('html, body').animate({ scrollTop: $(scrollToHeading).offset().top - 20 });
                document.getElementById(checkHeading).className = "orderChecked";
            });
    });
};

2 Answers 2

1

Refactor using the same function for all buttons

Change the button to this:

<a data-step="1" class="buttonShowStep">

And add this function:

$(".buttonShowStep").click(function() {
    var self = $(this);
    var step = self.data("step");
    if (step == 1) return;
    var prevStep = step - 1;
    self.hide("blind", 200);
    $("#outerDivStep" + step ).show("blind", 300, function() {
        $('html, body').animate({ scrollTop: $(".scrollToHeading" + step).offset().top - 20 });
        document.getElementById("checkHeading" + prevStep).className = "orderChecked";
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

The enclosure in your code has side effect. Change to this:

function ButtonShowStep(i) {
    var buttonShowStep = "#buttonShowStep" + i;
    var outerDivStep = "#outerDivStep" + i;
    var scrollToHeading = ".scrollToHeading" + i;
    var checkHeading = "#checkHeading" + i -1;
    $(buttonShowStep).click(function() {
        $(this).hide("blind", 200);
        $(outerDivStep).show("blind", 300, function() {
            $('html, body').animate({ scrollTop: $(scrollToHeading).offset().top - 20     });
            document.getElementById(checkHeading).className = "orderChecked";
        });
   });
}

for (var i = 2; i <= 4; i++) {
    ButtonShowStep(i);
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.