1

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();
    }
}
2
  • 1
    Do you mean an else if clause? Commented Sep 10, 2009 at 4:37
  • On an unrelated note, you should consider using anonymous functions in your timers. Commented Sep 10, 2009 at 4:41

4 Answers 4

5

is this what you're looking for?

if(data == 'valid') {
    // block 1
} else if (data == 'concept') {
    // block 2
} else {
    // block 3
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm too tired to think. I put elseif, instead of else if. Thanks!
Whereas it was making elseif a function instead of the preset function "else if". :(
4

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.

1 Comment

I'm going to attempt this method, just to see how it runs.
3

you could use else if

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

Comments

2

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

2 Comments

Thanks. Can I ask what the advantage to adding more functions would be?
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!

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.