I have variable that is named as items. if I access it's value inside getjson function I get correct value. but if I access it's value outside of getJSON function I get string empty value. I need your help to understand what is problem. Thanks for any help! in advance
$("#CityId").on('click',function(e){
e.preventDefault();
var htmlText = "<ul class=ulList>";
var mainDiv = $('.divCounties');
var items = "";
for (var i = 0; i < arrCity.length; i++) {
htmlText += "<li class=ilceCaption>" + arrCity[i].text; // +"</li>";
htmlText += "<ul>";
$.getJSON("../Adds/GetCounties", {cityId: arrCity[i].value }, function(data){
$.each(data, function(i, state) {
items += state.Text;
//htmlText += "<li>" + state.Text + "</li>";
});
console.log(items); // ==> is getting correct value in this line**
});
htmlText += "</ul>";
htmlText += "</li>";
console.log(items); // ==> is getting string empty value in this line**
}
htmlText += "</ul>";
console.log(htmlText);
mainDiv.html(htmlText);
});