> How can access index i inside of "complete" method?
> The following gives me just the last value of i.
>
> var scripts = ['script1.js', 'script2.js'];
>
> for(var i=0; i<scripts.length; i++){
>
>         jQuery.ajax({
>
>                         dataType: 'script',
>                         url: scripts[i],
>                         complete: function (XMLHttpRequest, textStatus) {
>                                         debug('i = ' + i + '   complete:  
> textStatus = ' + textStatus);
>
>                         }
>                 });
> }


You need a closure that captures the value of i:

var scripts = ['script1.js', 'script2.js'];

$.each(scripts, function(i) {
    $.ajax({
        dataType: 'script',
        url: scripts[i],
        complete: function (xhr, status) {
            // ...
        }
    });
});


Reply via email to