jquery - $(this) not triggering in ajax success even after assigning value to DOM outside of ajax call -


following these instructions

i assigning this var element

var element = this; 

before ajax call works fine , called properly

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

if try , call again in success callback 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>')             }         }     }); }); 

this xy-problem.

you want change class - must use correct class name - used .refresh-stats since got error on .refreshstats.

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

also recommend change document in

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

to selector of static parent element since more effecient


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -