Hi guys so I am starting to learn how to make Ajax calls and I just ran into something and all the info for a solution is a little confusing, perhaps a little advanced for where I am.
So I successfully ran this ajax call to load in images from a folder and add them to an array. however, what if I wanted to call the array elements outside of the ajax function? for this console log I get a blank array, if I put the console log in the ajax call I get the desired result.
How can I use the array outside of the ajax call or am I missing something here?
$(document).ready(function(){
var folder = "imgs/";
var imgArr = [];
$.ajax({
url : folder,
success: function (data) {
$(data).find("a").attr("href", function (i, val) {
if( val.match(/\.(jpe?g|png|gif)$/) ) {
imgArr.push("<img src='"+ folder + val +"'>");
console.log("val: " + imgArr);
}
});
}
});
console.log("val: " + imgArr); // shows nothing
});