I've got a little ajax script that, on success, text is supposed to change.
$(document).on('click','.favorite', function(){
$URLtitle = $(this).attr('data-image-URLtitle');
$username = $(this).attr('data-username');
$token = $('meta[name=csrf-token]').attr("content");
$(this).text('test text 1');
$.ajax({
type: "POST",
url: host + '/' + $username + '/' + $URLtitle + '/favorite/',
data: {username: $username, URLtitle: $URLtitle, _token:$token},
success: function(res) {
$(this).text('test text 2'); alert('success');
}
});
if ( $(this).attr('data-fav') == 0) {
$(this).data('data-fav', 1);
} else if ( $(this).attr('data-fav') == 1) {
$(this).data('data-fav', 0);
}
});
As you can see, there's two $(this).text
in this example, one at the start and one after success.
However, the text becomes "test text 1" and not "test text 2" even after I get the alert of success. What's going on?