var questions = [];
$(document).ready(function() {
$.ajax({
type:"GET",
url:"question.xml",
dataType: "xml",
success: function(data) {
$(data).find('item').each(function() {
var question = $(this).find('question').text();
var answer = $(this).find('qid').text();
var opt1 = $(this).find('ans1').text();
var opt2 = $(this).find('ans2').text();
var opt3 = $(this).find('ans3').text();
var opt4 = $(this).find('ans4').text();
questions.push({'question':question, 'answer':answer, 'opt1':opt1, 'opt2':opt2, 'opt3':opt3, 'opt4':opt4});
});
alert(questions.length); // here it shows length as 20
}
});
alert(questions.length); // here it shows length as 0 });
I have an array declared as global (questions), the problem is when I access the array inside the ajax success it has 20 elements, but when I try to access out the array length becomes 0.
Can someone explain what I'm doing wrong.