Skip to content Skip to sidebar Skip to footer

Accessing Data Outside Of Ajax Call In Jquery

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(){

Solution 1:

$.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);
    });
});

Post a Comment for "Accessing Data Outside Of Ajax Call In Jquery"