Question

Is it possible to put multiple IF statements in Javascript? If so, I'm having a fair amount of trouble with the statement below. I was wondering if you can put another IF statement in between if (data == 'valid') AND else? I want to add another if data =='concept') between the two.

if (data == 'valid') {
    $("#file").slideUp(function () {
        $("#file").before('<div class="approvedMessage">WIN WIN WIN!</div>');
        setTimeout(ApprovedProof, 5000);
    });

    function ApprovedProof() {
        $("#file").slideDown();
        $('.approvedMessage').fadeOut();
    }

}
else {

    $("#file").slideUp(function () {
        $("#file").before('<div class="deniedMessage">NO NO NO!</div>');
        setTimeout(DeniedProof, 5000);
    });

    function DeniedProof() {
        $("#file").slideDown();
        $('.deniedMessage').fadeOut();
    }
}
Was it helpful?

Solution

is this what you're looking for?

if(data == 'valid') {
    // block 1
} else if (data == 'concept') {
    // block 2
} else {
    // block 3
}

OTHER TIPS

If you intend for the data variable to hold many different values, you may find the switch statement a better fit for your needs:

switch(data)
{
  case "valid":
     //stuff
     break;
  case "concept":
     //stuff
     break;
  case "this": case "that":
     //more stuff
     break;
  default:
     break;
}

Note that break statements after each case, that multiple case statements can be used per condition, and that there is a catch-call default case that will fire if nothing else matched. The switch statement is nothing more than a more concise version of the If statement.

you could use else if

if (data == "valid") {
    // stuff here
} else if (data == "concept") {
    // more stuff
} else {
    // other stuff
}

Unrelated to the question, but Joey asked for clarification of using an anonymous function with his timer:

if (data == 'valid') {
    $("#file").slideUp(function () {
        $("#file").before('<div class="approvedMessage">WIN WIN WIN!</div>');
        setTimeout(function () {
            $("#file").slideDown();
            $('.approvedMessage').fadeOut();
        }, 5000);
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top