I'm writing a JS file that deals with the client events in an HTMl page. Register form is one of these events ,so the user inserts his name and password and when clicking the submit button ,JS file must deal with this event.
var registerForm = document.getElementsByClassName('register-form');
registerForm[0].addEventListener('submit',function(e) {
var username = document.getElementsByClassName('register-form-username')[0].value;
var userpass = document.getElementsByClassName('register-form-pass')[0].value;
var userDetails={
name : username,
pass: userpass
};
console.log(JSON.stringify(userDetails)); /*this is working and it print out on the console the same name and pass the user entered*/
globalvar.register(userDetails);
});
var globalvar= {
register : function(userDetails) {
console.log(JSON.stringify(userDetails)); //this does not print in the console and it refreshes the page and reset the form fields
var x={
comment : "",
userDetails: {
name : userDetails.name ,
email : userDetails.pass
}
};
console.log(JSON.stringify(x));
localStorage.setItem('localStor' , JSON.stringify(x));
}
};
what am I missing?