views:

28

answers:

3
$('.myElem').live('click', function() {
    $(this).hide(500, function() {
        $(this).siblings('.myOtherElem').show();
    });
});

The above doesn't work because $(this) is no longer in correct scope in the callback. How do I pass my original source element into the callback?

+2  A: 
$('.myElem').live('click', function() { 
    var $this = $(this);
    $this.hide(500, function() { 
        $this.siblings('.myOtherElem').show(); 
    }); 
}); 
John Hartsock
+4  A: 

Your could should work.

To access this within a inner method you have to store the reference in the outer method like this:

$('.myElem').live('click', function() {

   var myElem = this;

    $(this).hide(500, function() {
        $(myElem ).siblings('.myOtherElem').show();
    });

});
Ghommey
A: 
$('.myElem').live('click', function() {
    $(this).hide(500);
    $(this).siblings('.myOtherElem').show();
});
mcandre
That won't accomplish the same thing. Putting the `.show()` call in the callback ensures that it won't happen until the `.hide()` animation is completed. The code in your answer will cause them to both happen nearly simultaneously.
Daniel Schaffer
You could do it by using `delay`: `.delay(500).show(1)` However using the `show` callback is the better solution.
Ghommey