2

Edit - Ive changed my code:

This works fine:

$(".image-div").click(function () {
        if ($(this).css('z-index') == 1) 
            $(this).css('z-index','2');
        else 
            $(this).css('z-index','1');
});

And this also works:

    $(".image-div").click(function () {
        if ($(this).css('z-index') == 1) 
            $(this).animate({
                width: '500px'
            }, 5000, function() {
                // Animation complete.
            });
        else 
            $(this).animate({
                width: '250px'
            }, 5000, function() {
                // Animation complete.
            });
    });

But this doesn't do anything:

    $(".image-div").click(function () {
        if ($(this).css('z-index') == 1) 
            $(this).css('z-index','2');
            $(this).animate({
                width: '500px'
            }, 5000, function() {
                // Animation complete.
            });
        else 
            $(this).css('z-index','1');
            $(this).animate({
                width: '250px'
            }, 5000, function() {
                // Animation complete.
            });
    });

Thanks

1
  • Check your if Closing and as well as closing anonymous functions Commented Jun 8, 2011 at 19:26

2 Answers 2

7

Yes. No beginning { or ending } after the if & before the else.

Also, it looks like a double }); at the bottom.

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

2 Comments

And consequently the if is only being applied to this line: ` $(this).css('z-index','2');` which is why that works but the other doesn't.
Yes, that isn't necessary a syntax error in itself. But the else is now unexpected which is a syntax error.
2

Looks like you need curly braces around your if...otherwise, that else is floating with no connected if.

[ADDED] Okay, curly braces are needed for ANY controls with multiple statements following. The following works fine without curly braces because there's only one statement after each control:

if( condition ) doSomething();
else doSomethingElse();

However, this will break:

if( condition ) 
  doSomething();
  doAnotherThing();
else
  doSomethingElse();

Why? Because the IF in this case only covers this:

if( condition ) 
  doSomething();

Then, you have this sitting out in the middle of nowhere:

  doAnotherThing();
else
  doSoemthingElse();

doAnotherThing is NOT contained inside the if(), and thereby neither is your else{}. So, your else loses its attachment to the if and the code breaks. It is, IMHO, best practice to ALWAYS use curly braces, because if you don't, you or someone else may come in and add the doAnotherThing() line to the code thinking it's contained in the if() and inadvertently break the code.

That said, your if/else both contain multiple statements. So, from strictly a syntax point, your code needs to be:

$(".image-div").click(function () {
    if ($(this).css('z-index') == 1) { 
        $(this).css('z-index','2');
        $(this).animate({
            width: '500px'
        }, 5000, function() {
            // Animation complete.
        });
    }
    else {
        var divRef = this;
        $(this).animate({
            width: '250px'
        }, 5000, function() {
            $(divRef).css('z-index','1');
        });
    }
});

4 Comments

where do I need braces? Ive updated my answer but ive tried curly braces everywhere I could think off with no luck. thanks
Okay, added an explanation for why you needed curly braces and the change I would make to your code syntax-wise. Obviously, still can't guarantee that it works, but it should be syntactically correct.
It does work but I have a follow up question. The z-index change occurs after the animation is run, can the order be changed? I do want the z-index change to run after the else animation, but I need to happen before the if animation. Thanks again.
Changed the else in my code above so that it should change the z-index when animation completes.

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.