For some reason my values are not being stored in the array:
var req = new Array();
$.get('./ajax/get_cat_info.php?cid=' +cid, function(data, textStatus) {
var count = 0;
$.each(data, function(key, val) {
$('#' + key).show();
if(val == 1) {
req[count] = key;
count = count + 1;
//var arLen=req.length;
//alert('l: ' + arLen); // this works though
}
});
}, 'json');
var arLen=req.length;
alert('l: ' + arLen);
I get alerted “l: 0” at the end. If I uncomment the line alert in the IF statement, it alerts on each one, then still alerts 0.
The
getcall is running asynchronously, and soarLen=req.lengthis being evaluated prior to the function of elements being set actually completing. You can set the values accordingly from within the callback of the async call, as you determined.