I have a empty array named questions and I used the $.get
method to add objects to the array. inside $.get
, array is filled with objects and works fine. but once outside of $.get
, array becomes empty again, as array.length
returns 0
.
var questions=[];
var input;
$(document).ready(function(){
$.get('songs.html', function(list){
var cat = $(list).find('li');
var dog = [];
var count = 0;
var n;
cat.each(function(i, li){
dog[count] = $(li).text();
count = count + 1;
if(count == 3){
n = Math.floor(Math.random()*4);
questions.push({"song":"","choice1":dog[0],"choice2":dog[1],"choice3":dog[2],"choice4":dog[3], "answer":dog[n]});
count = 0;
}
});
console.log(questions.length);//array has length
});
console.log(question.length);