1

Following these instructions

I am assigning this to a var element

var element = this;

Before the AJAX call this works fine and is called properly

$(element).parent('.refreshstats').html('<i class="fa fa-refresh refresh-stats fa-spin" id="'+id+'" url="'+url+'"></i>');

If I try and call it again in the success callback it does not trigger

$(element).parent('.refreshstats').html('<i class="fa fa-refresh refresh-stats" id="'+id+'" url="'+url+'"></i>');

JQUERY

$(document).on('click', '.refresh-stats', function() {
    var id = $(this).attr('id');
    var url = $(this).attr('url');
    var element = this;
    $(element).parent('.refreshstats').html('<i class="fa fa-refresh refresh-stats fa-spin" id="'+id+'" url="'+url+'"></i>');
    $.ajax({
        type: "POST",
        url: "refresh-stats.php",
        data:({id: id, url: url}),
        success: function(data) {
            $(element).parent('.refreshstats').html('<i class="fa fa-refresh refresh-stats" id="'+id+'" url="'+url+'"></i>');
            var result = $.parseJSON(data);
            if (result[0] == 'No') {
                $('.indexed[data-pk="'+id+'"]').html('<span class="label label-danger">No</span>')
            }
            if (result[0] == 'Yes') {
                $('.indexed[data-pk="'+id+'"]').html('<span class="label label-success">Yes</span>')
            }
            if (result[1] == 'No') {
                $('.dindexed[data-pk="'+id+'"]').html('<span class="label label-danger">No</span>')
            }
            if (result[1] == 'Yes') {
                $('.dindexed[data-pk="'+id+'"]').html('<span class="label label-success">Yes</span>')
            }
            if (result[2] == 'No') {
                $('.removed[data-pk="'+id+'"]').html('<span class="label label-danger">No</span>')
            }
            if (result[2] == 'Yes') {
                $('.removed[data-pk="'+id+'"]').html('<span class="label label-success">Yes</span>')
            }
        }
    });
});
Community
  • 1
  • 1
Cesar Bielich
  • 4,754
  • 9
  • 39
  • 81

2 Answers2

2

This is an XY-problem.

You likely want to just change class - also you MUST use the correct class name - I used .refresh-stats since you got an error on .refreshstats.

$(document).on('click', '.refresh-stats', function() {
    var id = this.id;
    var url = $(this).attr('url');
    var $element = $(this); // store the jQuery object
    $(this).addClass("fa-spin"); // set the spinner
    $.ajax({
        type: "POST",
        url: "refresh-stats.php",
        data:({id: id, url: url}),
        success: function(data) {
            $element.removeClass("fa-spin"); // remove the spinner

Also I recommend to change the document in

$(document).on('click', '.refresh-stats', function() {

to the selector of a static parent element since it is more effecient

mplungjan
  • 169,008
  • 28
  • 173
  • 236
2

Before you make the request you are replacing the html of element.parent()

At that point element no longer exists in the DOM

Suggest you store reference to the parent instead since it is not being removed

$(document).on('click', '.refresh-stats', function() {
    var id = $(this).attr('id');
    var url = $(this).attr('url');
    var $parent = $( this).parent('.refreshstats');// don't really need class for `parent()`
    $parent.html('......');
    $.ajax({
        type: "POST",
        url: "refresh-stats.php",
        data:({id: id, url: url}),
        success: function(data) {
            $parent.html(......
charlietfl
  • 170,828
  • 13
  • 121
  • 150