Trying to extract statistics from my XML based on a specific name request, but encountering issues with my JavaScript functionality.
XML data:
<player>
<forward><name>Joe</name><stats>45</stats></forward>
<forward><name>Jack</name><stats>42</stats></forward>
<forward><name>Peter</name><stats>34</stats></forward>
<forward><name>Steve</name><stats>21</stats></forward>
<goalie><name>Pat</name><stats>2.34</stats></goalie>
</player>
HTML setup (ajax):
<html>
<head>
<script language="JavaScript">
function LoadDoc(vValue) {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "player.xml", true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
Answer(xmlhttp.responseXML, vValue);
}
}
xmlhttp.send(null);
}
function Answer(doc, ParamValue) {
var counts=doc.getElementsByTagName("forward");
for(var i=0;i < counts.length; i++){
alert(counts.length)
var vname = counts[i].getElementsByTagName('name');
alert(vname[i].firstChild.nodeValue)
var vstats = counts[i].getElementsByTagName('stats');
alert(vstats[i].firstChild.nodeValue);
if (vname[i].firstChild.nodeValue == ParamValue)
{
alert(stats[i].firstChild.nodeValue);
}
}
}
</script>
</head>
<body>
<form>
<input type="field" id="champ" />
<input type="button" onclick="javascript:LoadDoc(document.getElementById('champ').value);" />
</form>
</body>
</html>
The first ALERT correctly identifies 4 elements
The second ALERT successfully displays 'JOE'
The third ALERT correctly shows '45' (the stats of the first forward)
However, the alerts are only triggered once... Why isn't it looping 4 times as expected? Any insights on what might be causing this issue? Is there a better approach to retrieve the statistics?