Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
akash devgan
2,312 PointsI want to add input values to database array on click of button.
I want to add input values to database array on click of button. But every time I enter value and click add button array is updated as well because of what previous value is deleted in array.
Here is the code//
var addButton = document.getElementById("add"); var username = document.getElementById("name"); var telephone = document.getElementById("telephone");
addButton.onclick = validInformation;
//this function is validating all the user information function validInformation (){ var database = []; username1 = username.value.trim(); telephone1 = telephone.value.trim();
//check if user actually enter information
if(username1.length !== 0 && telephone1.length !== 0){
//checking if user entered valid information
if(typeof username1 === "string" && isNaN(telephone1) === false){
// if about condition are correct add information to database
database.push(username1);
alert(database.length);
}else{
alert("please enter a valid information");
}
}else{
alert("please enter the complete infor");
}
}
1 Answer
Steven Parker
243,656 PointsIf you want the database array to accumulate new values, you need to move the definition out of the function and make it global.
Defining it inside the function limits it's scope to only while the function is running, and guarantees that it will start empty every time the function is called.