Marcel Wasilewski Marcel Wasilewski - 4 months ago 15
Ajax Question

Get Value of Ajax changed field

I am making an ajax call, it looks like this:

$.ajax({
url: "index.php?id=70&type=999",
type: 'post',
data: form + '&sort=' + val,
success: function(response)
{
$(".listing").load(location.href + " .listing");
$(".count").load(location.href + " .count");
},
complete: function (response) {
alert( $(".count").val());
},
error: function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
}
});


So, as you can see 2 classes "listing" and "count" are changing, this works fine. After i am trying to alert the NEW value of the "count" class, but it is always giving me the actual one. And just after the alert, the value changes. But doesn't
complete:
mean that the call is made after the
success:
function? Why the alert is being made first and gives me the old value before the ajax call?

Answer

Try changing you're code like this:

$.ajax({
                    url: "index.php?id=70&type=999",
                    type: 'post',
                    data: form  + '&sort=' + val,
                    success: function(response)
                    {
                        $(".listing").load(location.href + " .listing");
                        $(".count").load(location.href + " .count", function(){
                              alert( $(".count").val());
                         });
                    },
                    complete: function (data) {

                    },
                    error: function(xhr, ajaxOptions, thrownError){
                        alert(xhr.status);
                    }
                });
Comments