I have the following code:

```
<script>
function initialData() {
    var patches = [<?php echo $jsPatchArray ?>];

    for (i = 0; i < patches.length; i++) {

        var divIdPatch = "\"#initialData-" +patches[i]+ "\"";   

        $.ajax({
            type: "GET",
            url: "initial_data.php",
            data: {patch:patches[i]},
            success: function( data ) {
                $( divIdPatch ).html( data );
            }
        });

    };

    var divIdCount = "#srCount";

    $.ajax({
        type: "GET",
        url: "sr_count.php",
        success: function( data ) {
            $( divIdCount ).html( data ); 
        }
    });

}
</script>

<?php
$_SESSION['patchObjs'] = array();

foreach ($arrayRelQA as $patch)
{
    echo "\n";
    echo '<div id="initialData-'.$patch.'"></div>';
}
echo "\n<div id=\"srCount\"></div>";

```

From the above this part works fine:

```
var divIdCount = "#srCount";

$.ajax({
    type: "GET",
    url: "sr_count.php",
    success: function( data ) {
        $( divIdCount ).html( data ); 
    }
});

```

However the loop section does not - the ajax and the underlying `initial_data.php` is being executed however the success is not finding my div (`divIdPatch`). Must be something to do with how I'm constructing the var `divIdPatch`. I've tried the following (and various other variants:

```
var divIdPatch = "\"#initialData-" +patches[i]+ "\"";   
var divIdPatch = "#initialData-" +patches[i];;

```

Subsequent investigation / edit
-------------------------------

It looks like the ajax is messing with the for loop:

```
for (i = 0; i < patches.length; i++) {

    var patch = patches[i];
    var divIdPatch = "#initialData-" +patches[i]; 

    alert("Outside: "+patch );
    alert( "Outside: "+divIdPatch );        

    $.ajax({

        type: "GET",
        url: "initial_data.php",
        data: {patch:patch},
        success: function( data ) {

            alert( "Inside Div: "+divIdPatch);
            alert( "Inside Data: "+data);
            $( divIdPatch ).html( data );
        }
    });

};

```

The alerts fire in this order (3 pacthes in the array)
Note the patch order on the 'Inside Div' alert:

```
Outside: patch 1
Outside: #initialData-patch 1
Inside Div: #initialData-patch 2
Inside Data: Patch 1 complete.

Outside: patch 2
Outside: #initialData-patch 2
Inside Div: #initialData-patch 3
Inside Data: Patch 2 complete.

Outside: patch 3
Outside: #initialData-patch 3
Inside Div: #initialData-patch 3
Inside Data: Patch 3 complete.

```