I need to save data in a global variable, so that I can use it from different function of application.
Here is the code I tried.
var userDetails=[];
function getDetails(){
var firstname=document.getElementById('firstname').value;
var lastname=document.getElementById('lastname').value;
var email=document.getElementById('email').value;
var password=document.getElementById('password').value;
var item = {}
item ["firstname"] = firstname;
item ["lastname"] = lastname;
item ["email"] = email;
item ["password"] = password;
userDetails.push(item);
}
The problem is when I am trying to access it from a different function, then I am getting userDetails
as undefined.
I am accessing the userDetails
from another function after executing getDetails()
.
UPDATE
I am trying to access userDetails
from the below function. NOTE: I am calling getDetails
on SignUp button, and for the login I am calling this.
function login(){
var userName=document.getElementById('loginUserName').value;
var password=document.getElementById('loginPassword').value;
console.log(userDetails); // here i need userDetails to check if this user has signed up and credential are correct.
}