var $score;
var $correct;
var $answered;
var $percentage;
var $stats=[$score,$correct,$answered,$percentage];
Using a for-loop, I then assign values for these variables based on their corresponding XML elements' text node values.
I wanted to see if this worked, so I created a <p> element in the HTML page and instructed jQuery to display the value of the $score variable.
I did not see a "0" posted anywhere in the HTML page.
I would like to know where I went wrong with the code.
XML file
<?xml version="1.0" encoding="utf-8"?> <stats> <jeopardy> <score>0</score> <correct>0</correct> <answered>0</answered> <percentage>0</percentage> </jeopardy> <jeopardy> <score>0</score> <correct>0</correct> <answered>0</answered> <percentage>0</percentage> </jeopardy> <jeopardy> <score>0</score> <correct>0</correct> <answered>0</answered> <percentage>0</percentage> </jeopardy> </stats>
jQuery file
var $score;
var $correct;
var $answered;
var $percentage;
var $stats=[$score,$correct,$answered,$percentage];
function importData(xml){
var $i=0;
var $k=0;
var $length=$(xml).find('jeopardy').eq(1).children().length;
while ($i<$length && $k<$length){
$stats[$k]=$(xml).find('jeopardy').eq(1).children().eq($i).text();
$i++;
$k++;
}
}
$.ajax({
type: 'GET',
url: 'playerstats.xml',
dataType: 'xml',
success: importData
});
$('p').text($score);
HTML
<!DOCTYPE html> <html> <head> </head> <body> <p></p> <script src="jquery-2.1.4.min.js"></script> <script src="extract.js"></script> </body> </html>

New Topic/Question
Reply


MultiQuote



|