0

I have developed following code, I need to pass exact 'this' value ( because lot of items with this class)in to ajax success function. How to do that.

$(document).on('click', '.address_remove_link', function(e) {
    var id = $(this).attr("data-id");
    $.ajax({
        type: 'POST',
        url: 'inc/controlllers/detele_shipping_addr.php',
        data: {
            internalId: id
        },
        success: function(response, this) {
            $(this).parent().closest('div').hide(200);
        },
        error: function(data) {
            console.log("error");
        }
    });
});
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
hogard
  • 29
  • 1
  • 9
  • `this` always refers to the calling object in the current scope. To access the outer scope, the typical way is to store the outer `this` in a variable like `var that = this;` or `var self = this;` or use binding, as demonstrated by [this article](http://jsforallof.us/2014/07/08/var-that-this/). – SNag Sep 13 '16 at 08:57
  • @SNag: *"`this` always refers to the calling object in the current scope"* No, it doesn't. For non-arrow, non-bound functions, `this` means whatever the calling code made it mean, which can be anything. – T.J. Crowder Sep 13 '16 at 09:27

3 Answers3

1

The issue is because the scope of this changes within the success handler function. You can store the outer scope in the click handler function instead. Try this:

$(document).on('click', '.address_remove_link', function(e) {
    var $link = $(this); // store reference here
    var id = $link.attr("data-id");

    $.ajax({
        type: 'POST',
        url: 'inc/controlllers/detele_shipping_addr.php',
        data: {
            internalId: id
        },
        success: function(response) {
            $link.parent().closest('div').hide(200); // to use here
        },
        error: function(data) {
            console.log("error");
        }
    });
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

One simple way is to use a variable the function closes over:

$(document).on('click', '.address_remove_link', function(e) {
    var $this = $(this);                                  // ***
    var id = $this.attr("data-id");                       // ***
    $.ajax({
        type: 'POST',
        url: 'inc/controlllers/detele_shipping_addr.php',
        data: {
            internalId: id
        },
        success: function(response) {
            $this.parent().closest('div').hide(200);      // ***
        },
        error: function(data) {
            console.log("error");
        }
    });
});

Alternately you could use Function#bind (MDN, spec):

$(document).on('click', '.address_remove_link', function(e) {
    var id = $(this).attr("data-id");
    $.ajax({
        type: 'POST',
        url: 'inc/controlllers/detele_shipping_addr.php',
        data: {
            internalId: id
        },
        success: function(response) {
            $(this).parent().closest('div').hide(200);
        }.bind(this),                                     // ***
        error: function(data) {
            console.log("error");
        }
    });
});
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

You are working on the scope of the click event handler function, so you can create a self variable and use it like this:

$(document).on('click', '.address_remove_link', function(e) {
    var self = $(this),
        id = self.attr('data-id');

    $.ajax({
        type: 'POST',
        url: 'inc/controlllers/detele_shipping_addr.php',
        data: {
            internalId: id
        },
        success: function(response) {
            // Do something with "response"
            self.parent().closest('div').hide(200);
        },
        error: function(data) {
            console.log('error');
        }
    });
});
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46