jQuery get source element in callback

$('.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?

Answers


Actually your code should work.

To access this within an inner javascript method you might store the reference in the outer method scope:

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

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

});

However in most jQuery methods this is referring to the selector or element used:

$('.myElem').on('click', function() {
    // This refers to the clicked element
    $(this).hide(500, function() {
       // This refers to the clicked element as well 
       $(this).siblings('.myOtherElem').show();
    });    
});

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

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

Need Your Help

Linkage error in WAS 8.5.5

web-services rest jersey eclipselink websphere-8

I am trying to deploy jersey RESTFUL webservice on WAS 8.5.5 but receiving following error

java fatal exception for android program

java android authentication cifs jcifs

i got the following exception when i run the code below. this is an android code, to access remote windows pc. it is used for download, upload and delete.when i run this code its throwing verificat...