I have problems with .stringify()
, but I think my JavaScript array must be wrong, here\'s my code:
var questions = new Array();
$(\'#Valid\').hover
Arrays have integer keys, not strings.
Use an object instead; objects in JS sort of look like associative arrays:
var questions = new Array();
$('#Valid').hover(function(){
for (var i=0;i < $('.Questions').length;i++){
questions[i]={};
questions[i]['numero']=$('.Numero:eq('+i+')').html();
questions[i]['question']=$('.ItemInput:eq('+i+')').val();
questions[i]['variable']=$('.VarName:eq('+i+')').val();
}
var stringJSON=JSON.stringify(questions);
alert(stringJSON);
});
Setting questions[i]
to {}
is the key.
You can shorten this syntax:
var questions = new Array();
$('#Valid').hover(function(){
for (var i=0;i < $('.Questions').length;i++){
questions[i] = {
numero: $('.Numero:eq('+i+')').html(),
question: $('.ItemInput:eq('+i+')').val(),
variable: $('.VarName:eq('+i+')').val()
};
}
var stringJSON=JSON.stringify(questions);
alert(stringJSON);
});