views:

233

answers:

4

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();
    }
}
+4  A: 

is this what you're looking for?

if(data == 'valid') {
    // block 1
} else if (data == 'concept') {
    // block 2
} else {
    // block 3
}
Jonathan
I'm too tired to think. I put elseif, instead of else if. Thanks!
Homework
Whereas it was making elseif a function instead of the preset function "else if". :(
Homework
@Joey: Don't look now, but your VB is showing
Ken Browning
I think I noticed that...
Homework
+3  A: 

you could use else if

if (data == "valid") {
    // stuff here
} else if (data == "concept") {
    // more stuff
} else {
    // other stuff
}
nickf
+3  A: 

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.

David Andres
I'm going to attempt this method, just to see how it runs.
Homework
+2  A: 

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);
    });
}
Kevin
Thanks. Can I ask what the advantage to adding more functions would be?
Homework
That's not adding more functions, it's declaring the function where it's used as an 'anonymous function', aka a lambda or closure. There's no point in declaring it separately and giving it a name since you only use it right in that spot. Also, closures have some unique and powerful properties regarding variable scope which are very important in advanced JavaScript, but that's a whole other issue!
Alex JL
Clearly understandable.
Homework