skip to Main Content

I need help in localStorage in JS. I want a program that takes input from my website (of course not registered domain) and saves the input . My code of HTML is the following :

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body bgcolor="lightyellow">
    <CENTER>
        <h1>Registeration Form</h1>
    </CENTER>
    <hr>
    <h2>Username :</h2>
    <input type="text" id="name">
    <br>
    <h2>Password: </h2>
    <input type="password" id="pass">
    <br>
    <button type="button" id="btn">SUBMIT</button>
    <script src="e.js"></script>
</body>
</html>

I tried storing the value using JS but it didnt work.

document.getElementById("btn").addEventListener("click", function() {
name = document.getElementById("name").value;
pass = document.getElementById("pass").value;
localStorage.setItem(name , "name");
localStorage.setItem(pass , "pass");
console.log(localStorage.getItem(name));
console.log(localStorage.getItem(pass));
})

I was of course doing something wrong here.
PLs help me

2

Answers


  1. You have passed wrong order of parameters inside localstorage

    document.getElementById("btn").addEventListener("click", function() {
    name = document.getElementById("name").value;
    pass = document.getElementById("pass").value;
    localStorage.setItem("name" , name);
    localStorage.setItem("pass" , pass");
    console.log(localStorage.getItem(name));
    console.log(localStorage.getItem(pass));
    })
    
    
    Login or Signup to reply.
  2. MDN Window.localStorage

    localStorage.setItem("key" , value);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search