I'm using jQuery to parse an XML file, and I'm trying to push each element in the XML file to an array using a jQuery .each loop. Strangely, if I alert the value of the array within the loop it comes out as it should, but if I try to alert a value in the array after the loop has finished it results in "undefined".
Is there something strange that happens when you push values to an array within this kind of a loop?
Here is the Javascript:
var splashArray = new Array();
// Load the Splash XML file and assign each image within to an array
$.get('splash.xml', function(xml) {
$('image', xml).each(function (i) {
splashArray.push($(this).attr("src"));
});
});
alert(splashArray[1]); // Results in undefined
Here is the XML:
<?xml version="1.0" encoding="UTF-8"?>
<site>
<image src="splash1.jpg" />
<image src="splash2.jpg" />
<image src="splash3.jpg" />
<image src="splash4.jpg" />
<image src="splash5.jpg" />
<image src="splash6.png" />
</site>