I'm trying to a code a script that 1) Reads FB user IDs from text input fields & loops them into an array 2) calls FB.api with each of the IDs for first name and saves the returned first name value into another array. The problem: for some reason, all the first names received from FB.api save to the same array index place 5. Which is extra weird since the loop should only go from indexes 1 to 4.
<script type="text/javascript">
var ids = new Array();
var names = new Array();
var fields = 4;
function firstNames() {
var i;
var k;
for (i=1;i<=fields;i++)
{
ids[i] = document.getElementById("field"+i).value;
}
for (k=1; k<=fields; k++)
{
FB.api('/'+ids[k], function(response) {
names[k] = response.first_name;
alert(k+' test '+names[k]);
});
}
}
</script>
Has anyone ran into something similar? Any help would be much appreciated.
Thanks beforehand.