Tim Roering
Tim Roering

Reputation: 21

Getting multiple if statements

I'm making an interactive series for my school project. We have some voice overs on the video's. So we have male and female voices. So I made a page with a button where they can choose between a male (man) and female(vrouw). the first if statement works like a charm. ( choosing between a male and female voice) after this, the user can choose between 2 choices in the video. But "choiceaman" doesn't work at all. Anyone an idea how I can make it work better. (FYI I'm not a coder usually so this is all new for me)

$(document).ready(function() {
            if ($.cookie('keuzemanvrouw') == "man") {
                $("#pilotman").show();
                $("#pilotvrouw").hide();
                $("#choiceavrouw").hide();
                $("#choicebvrouw").hide();
                $("#choiceaman").hide();
                $("#choicebman").hide();
                $("#keuzemanvrouw").hide();
            }

            else if ($.cookie('keuzemanvrouw') == "vrouw") {
                $("#pilotvrouw").show();
                $("#pilotman").hide();
                $("#choiceaman").hide();
                $("#choicebman").hide();
                $("#choiceavrouw").hide();
                $("#choicebvrouw").hide();
                $("#keuzemanvrouw").hide();

            } else if ($.cookie('choice1') == "choiceaman"){
                $("#choiceaman").show();
                $("#choicebman").hide();
                $("#pilotman").hide();
            }

            else if ($.cookie('choice1') == "choiceavrouw"){
                $("#choiceavrouw").show();
                $("#choicebvrouw").hide();
                $("#pilotvrouw").hide();
            }


            else if ($.cookie('choice1') == "choiceman"){
                $("#choicebman").show();
                $("#choiceaman").hide();
                $("#pilotman").hide();
            }



            else {
                $("#pilotvrouw").hide();
                $("#pilotman").hide();
                $("#choiceavrouw").hide();
                $("#choiceaman").hide();
                $("#choicebvrouw").hide();
                $("#choicebman").hide();
            }
        });

Upvotes: 0

Views: 46

Answers (1)

Ethan
Ethan

Reputation: 4375

Try this instead:

$(document).ready(function() {
    if ($.cookie('keuzemanvrouw') == "man" || $.cookie('keuzemanvrouw') == "vrouw" || $.cookie('choice1') == "choiceaman" || $.cookie('choice1') == "choiceavrouw") {

        if ($.cookie('keuzemanvrouw') == "man") {
            $("#pilotman").show();
            $("#pilotvrouw").hide();
            $("#choiceavrouw").hide();
            $("#choicebvrouw").hide();
            $("#choiceaman").hide();
            $("#choicebman").hide();
            $("#keuzemanvrouw").hide();
        } else if ($.cookie('keuzemanvrouw') == "vrouw") {
            $("#pilotvrouw").show();
            $("#pilotman").hide();
            $("#choiceaman").hide();
            $("#choicebman").hide();
            $("#choiceavrouw").hide();
            $("#choicebvrouw").hide();
            $("#keuzemanvrouw").hide();

        }

        if ($.cookie('choice1') == "choiceaman") {
            $("#choiceaman").show();
            $("#choicebman").hide();
            $("#pilotman").hide();
        } else if ($.cookie('choice1') == "choiceavrouw") {
            $("#choiceavrouw").show();
            $("#choicebvrouw").hide();
            $("#pilotvrouw").hide();
        }

    } else {
        $("#pilotvrouw").hide();
        $("#pilotman").hide();
        $("#choiceavrouw").hide();
        $("#choiceaman").hide();
        $("#choicebvrouw").hide();
        $("#choicebman").hide();
    }
});

Your if statements are all hanging off each other, so the choice1 cookie else ifs won't run if keuzemanvrouw is man or vrouw. else ifs only run if there are no other previous if statements that fired.

Think of it like this:

if (false) { // Does check this statement.
    // Doesn't run this code.
} else if (true) { // Does check this statement.
    // Runs this code.
} else if (true) { // Doesn't check this statement, because the previous one worked.
    // Doesn't run this code.
}

Upvotes: 1

Related Questions