2

Below is the code I am working on, my goal is to call the ajax to return some data and append that data on the button/$(this) that is clicked.

$('.click_me').click(function(){

    $.ajax({
        type: 'POST',
        url: 'ajax/get_list.php'
    }).done(function(data){
        $(this).append(data);
    });
});
Abhitalks
  • 27,721
  • 5
  • 58
  • 81
ILikebanana
  • 109
  • 1
  • 2
  • 10

1 Answers1

2

$.ajax returns an XHR object and it is the context which is calling the done method. Hence you need to store the context of button first before making the ajax and use that variable.

$('.click_me').click(function(){
    var $self = $(this);
    $.ajax({
        type: 'POST',
        url: 'ajax/get_list.php'
    }).done(function(data){
        $self.append(data);
    });
});
Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
  • That should work! The $(this) in your done() callback references the $.ajax object and has a different scope than as suggested here! – mahatmanich Nov 29 '13 at 09:25