I'm trying to build a function to add elements (taken from input fields) in array:
function printPeople(idUser){
var name = $('#name').val();
var surname = $('#surname').val();
var age = $('#age').val();
var people = [];
people.push({
"id": idUser,
"name": name,
"surname": surname,
"age": age
});
Everytime I press button "subscribe" I need to add new information in the array, my goal is obtain an array like this:
[{
"id": id,
"nome": name,
"cognome": surname,
"age": age
},
{
"id": id,
"nome": name,
"cognome": surname,
"age": age
}, etc]
In my case I created an empty array and then I add the elements, but everytime I press the button, array becomes empty again...the array doesn't exist outside of the function, so I need to create it at the beginning (and so the array will be empty everytime I call the function..). Any help?