So here’s my code for when a user clicks a follow button:
$('.follow_btn').click(function() {
$(this).html('<img src = "../assets/style_images/loading.gif">');
var userId = $(this).attr('id');
$.post('../assets/scripts/ajax_follow_parse.php', {
userId: userId
}, function(data) {
$(this).html(data);
});
});
It is quite happy to replace it with the loading gif as shown on line 2. But when it returns the data and replace it with the data returned on line 4.
How can I fix this?
Assign
$(this)to a variable outside the$.post():Then, instead of using
$(this)to add the data, use the variable we just created:Looking at your code again, you could also do this:
Since you already have the
idof your element.