I have an ajax code like that:
$.ajax({
type: "post",
async: false,
url: "/FindAVet/Search",
data: '{"vetname":"' + $("#VetName").val() + '","lat":"' + objlatitude + '","lng":"' + objlongitude + '","radius":"' + $("#hdnRadius").val() + '","searchAll":"' + searchAll + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (result.IsValid == true) {
$("#divMapContainer").show();
$("#placeholderServiceagent").html('');
$("#placeholderServiceagent").show();
$("#placeholderServiceagent").html(result.Datavalue);
$("#noserviceagentstatus").hide();
var arrLoc = result.Locations.split(";");
var arrInf = result.InfoWindowContents.split(";");
var source, destination, distance;
source = $("#SuburbOrPostcode").val();
var service = new google.maps.DistanceMatrixService();
var arr2dLoc = [];
var arr2dInf = [];
for (var i = 0; i < arrLoc.length; i++) {
arr2dLoc[i] = arrLoc[i].split(",");
}
var j = 1;
for (var i = 0; i < arrInf.length; i++) {
arr2dInf[i] = arrInf[i].split(",");
var strarr2dInf = arr2dInf[i].toString();
var dom_des = $($.parseHTML(strarr2dInf));
destination = dom_des.find('.des').text();
service.getDistanceMatrix({
origins: [source],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function (response, status) {
if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
distance = response.rows[0].elements[0].distance.text;
var spDistance = $("#distance" + j);
spDistance.prepend(distance);
var li = $("#" + j);
var distancewithoutkmtext = distance.replace(' km', '');
li.attr("id", distancewithoutkmtext);
j++;
} else {
alert("Unable to find the distance via road.");
}
});
}
init_map('map_canvas', 18, arr2dLoc, arr2dInf);
}
else {
$("#divMapContainer").hide();
$("#placeholderServiceagent").hide();
$("#noserviceagentstatus").show();
}
$("#lblServiceAgentStatus").html(result.Message);
},
complete: function (data) {
var elems = $('.storeList').children('li');
elems.each(function (idx, li) {
alert($(this).attr("id"));
});
}
I have already changed the id of li tag like that:
var li = $("#" + j);
var distancewithoutkmtext = distance.replace(' km', '');
li.attr("id", distancewithoutkmtext);
And the output html is rendered like that:
which is correct (with the new ids is updated)
But in complete function, when i try to test by calling alert function to show ids of <li>
tags, the old value of id is displayed yet like that:
which is wrong (1,2,3,4... are original values of id of li tags)
Can you help me guys? Any helps will be much appreciated. Thank you so much.