I am new to both stackoverflow and javascript(especially jquery and ajax). I have been trying to solve this problem from over a day and still cannot solve it. So I am trying to get a list of image names from a folder with this:
folder = 'image/';
function imageCollection(theImageList) {
$(document).ready(function () {
$.ajax({
url: folder,
success: function (data) {
$(data).find("a").attr("href", function (i, val) {
if (val.match(/\.(jpe?g|png|gif)$/)) {
$("#comparison").append("<img src='" + folder + val + "'>");
theImageList.push(folder+val);
}
});
}
})
})
}
imageCollection(theImageList)
var imgLength = theImageList.length; // returns 0 while if I type theImageList.length directly in the console it shows 54
I set theImageList global variable. It's very strange that when I type theImageList in the console, it shows that the length is 54 and I can access each file name with no problem. However, when I set var imgLength = theImageList.length (after my function of reading files) it gave me 0 and when I tried to access element, say theImageList[0], it returns undefined. Similarly, my images were shown to be added to my comparison div successfully, but when I tried to access them through $('img') outside of the function, again failure. I also tried return theImageList in my function but again failure.
So at this point I am really confused as to what happened to my code. It shows signs of working but still does not give me what I have been looking for. I am not what is the right question to ask here. Any suggestions are welcome!