0

Is there a way to run multiple if else statements from one event? For example, trying to run this, but it's only executing the first if else for the element with class "two" and not the second element which has class "three".

$(".hamburger-menu").click(function(){

    if ($(".two").hasClass("two-rotate")){
        $(".two").addClass("two-rotate-back").removeClass("two-rotate");
    } 
    else {
        $(".two").addClass("two-rotate").removeClass("two-rotate-back");
    }

    // This one won't execute
    if ($(".three").hasClass("three-rotate")){
        $(".three").addClass("three-rotate-back").removeClass("three-rotate");
    }
    else {
        $(".three").addClass("three-rotate").removeClass("three-rotate-back");
    }
});

I know the answer must be a simple one, but I've been searching for 30 minutes now and can't find the answer.

11
  • are you seeing any error message in your console? and does element with .three class actually exists? Commented Jun 6, 2015 at 9:38
  • 2
    How have you determined that the code is not executing? Commented Jun 6, 2015 at 9:41
  • To verify, put an alert("HERE"); after .two code finishes to make sure it is working or not. Maybe an error is raising? Also, you could try to put in alert($(".three").length) to make sure that it's finding it. Commented Jun 6, 2015 at 9:42
  • "Is there a way to run multiple if else statements from one event?" - Yes, there's nothing wrong with your logic. Have you stepped through it in the debugger? Commented Jun 6, 2015 at 9:42
  • 1
    There's no }); at the end of the handler. Is it missing? Or did you just omit it from the code you posted. Commented Jun 6, 2015 at 9:45

3 Answers 3

1

There is missing }); at the end and also the click event may not be correctly binding.

Here is the working plunkr :"http://plnkr.co/edit/6hBXDFLXoGkHFBgvmbbx?p=preview"

Sign up to request clarification or add additional context in comments.

Comments

0

You're not closing correctly your code.

Put the }); at the end.

When you need to debug some code, you could do something like that:

if ( someExpression ) {

    // logic here...
    console.log( 'passed through if' );   

} else {

    // logic here...
    console.log( 'passed through else' );
}

Comments

0

I know you figured out your problem, but I just wanted to mention that if the -rotate and -rotate-back classes are mutually exclusive as they appear to be in your code, you could replace those if statements with a couple of short .toggleClass statements:

$(".hamburger-menu").click(function(){
    $(".two").toggleClass("two-rotate-back two-rotate");
    $(".three").toggleClass("three-rotate-back three-rotate");
});

http://jsfiddle.net/UselessCode/hc4Ldm9n/

Comments

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.