Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
local storage being overwriten instead of adding to array

I am building a simple login web page with JavaScript and using Chrome. I have an area for login and one for registering a new user. My problem is, I have created an array and am storing it on the local storage. Every time a new registry happens its values override the previous ones on the Local storage. How can I make it so that the new users information is added to the array while keeping the previous ones? Code follows: `

const loginForm = document.getElementById("loginForm");
const loginButton = document.getElementById("loginButton");
const loginError = document.getElementById("loginError");

const registerForm = document.getElementById("registerForm");
const registerButton = document.getElementById( "registerButton");
const registerError = document.getElementById("registerError");
  
loginButton.addEventListener("click", (e)=> {
    e.preventDefault();

    const username = loginForm.username.value;
    const password = loginForm.password.value;
    /*WILL WORK ON THIS AFTER REGISTER IS OKAY*/
    if(username === "user" && password ==="123"){
        alert("Login sucessful!");
        location.reload();
    } else {
        loginError.style.opacity = 2;

    }
})

registerButton.addEventListener("click", (e)=> {
    e.preventDefault();

    const registerUsername = registerForm.registerUsername.value;
    const registerPassword = registerForm.registerPassword.value;
    const confirmPassword = registerForm.confirmPassword.value;
    const registerEmail = registerForm.registerEmail.value;

    const userData = {registerUsername, registerPassword};
    let registredUserData = [];
   

    if(registerPassword.length > 8 && registerPassword.match(/[a-z]+/) && registerPassword.match(/[A-Z]+/) && registerPassword.match(/[0-9]+/) &&  registerPassword.match(/[$@#&!]+/) && registerPassword === confirmPassword && registerEmail.match(/[@/]+/ ) ){      

        /*ERROR HERE.LOCAL STORAGE IS OVERWRITTEN*/ 
        JSON.parse(localStorage.getItem("registred users"));
        registredUserData.push(userData);
        localStorage.setItem("registred users",JSON.stringify(registredUserData));
            

        location.reload();
    } else {
        registerError.style.opacity = 2;

    }
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are not assigning a variable registredUserData in line after a comment. That section should probably look like this.

registredUserData = JSON.parse(localStorage.getItem("registred users"));
registredUserData.push(userData);
localStorage.setItem("registred users",JSON.stringify(registredUserData));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!