1

What I would like is very simple, but I am struggling with the syntax. When my element clicked, I would like my animate to be triggered and just after the .css(). Thank you in advance for your help. Cheers. Marc.

$('.content').click(function() {
    $('#contentWrapper').animate({
        "left": "-=475px"}, "fast"), 
    $(this).prev().css("display": "none");
});
2
  • Can you paste html here? Commented Jan 25, 2012 at 14:50
  • you DO know that .css("display","none") is the same as .hide() right? Commented Jan 25, 2012 at 15:20

3 Answers 3

4

Almost. You need to pass the complete argument on animate a function reference:

$('.content').click(function() {
    $('#contentWrapper').animate({
        "left": "-=475px"}, "fast", function () {
            $(this).prev().css("display", "none");
        }
    );
});

(Also, your syntax for css was a bit off, you wanted , instead of :)

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

5 Comments

Since javascript has function scope, if you wrap the call inside another function this points to another object and not to the clicked object (at least that's what i think)
Thank you very much for helping out Andrew. Works great!
@NicolaPeluchetti: That's correct, this in the callback will reference the animated object.
Andrew, why it is , instead of : I always use : for .css() and it works usually
@user1162116: You could also use .css({ "display": "none" })
2

You should provide the second function as a callback

$('.content').click(function() {
    $('#contentWrapper').animate({
        "left": "-=475px"}, "fast", $(this).prev().css("display": "none"));
    });
});

EDIT - I think that in this way this points to the clicked element. If it's not yous should do

$('.content').click(function() {
    var that = this;
    $('#contentWrapper').animate({
        "left": "-=475px"}, "fast", $(that).prev().css("display": "none"));
    });
});

Comments

1

You supply the second part as a callback function, that means that it executes after the first function is complete. If you were acting on the same elemnt as the first animation then you could use chaining ('I'm not sure if you can use it in this circumstance because you didn't supply the html).

$('.content').click(function() {
    $('#contentWrapper').animate(
        {"left": "-=475px"},
        "fast",
        function(){
            $(this).prev().css("display": "none");
        }
    );
});

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.