I use the $.get() function to process a lot of files and I need to find out the filename I am calling with from within its callback function. is there a way to do this?
while (allFilesToImport.length > 0) {
var fileToOperate = allFilesToImport.shift();
var jqxhr = $.get(path + '/' + fileToOperate,
function(data, textStatus, jqXHR){ // here I need the fileToOperate variable!});
You can access
fileToOperateandmypathToTheFileinside your function – that’s one of the reasons why closures are awesome.Here’s an example with a loop in case that’s what you have:
You could also use
$.each()to iterate over the array: