homework
homework

Reputation: 5087

Dealing with multiple Javascript IF statements

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();
    }
}

Upvotes: 1

Views: 6486

Answers (4)

Kevin
Kevin

Reputation: 3841

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);
    });
}

Upvotes: 2

David Andres
David Andres

Reputation: 31781

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.

Upvotes: 4

nickf
nickf

Reputation: 546055

you could use else if

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

Upvotes: 3

Jonathan Rupp
Jonathan Rupp

Reputation: 15762

is this what you're looking for?

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

Upvotes: 5

Related Questions