lukas13x
lukas13x

Reputation: 73

localStorage undefined?

Im doing a website project in my first year in college and i need to get local storage working. All i need is to input information of my website form and then display the local storage in another form on antoher page. i had it working but i added extra inputs, i made sure my ids and names were correct but it still wont work. Heres a part of all 4 files. I need this as it is right now but for some reason it wont work. I cant use anything new. !(http://i57.tinypic.com/9hkjfd.png)

<form action="readLocalStorage.html" method="post" name="form1" id="form1" onsubmit="return validateForm()">
            <fieldset>
                <legend>Sign Up</legend>
                <label for="username">Create a Username</label>
                <input type="text" placeholder="5 characters" name="username" id="username" required><br><br>

<form name="LSdata" id="LSdata" action="addContact.php" method="post">

        <label>Username</label>
        <input name="username" id="username" readonly /><br />

function writeLocalStorage() {
localStorage.username = document.form1.username.value;
}
function getLocalStorage() {
document.LSdata.username.value = localStorage.username;
}

Upvotes: 0

Views: 6802

Answers (1)

Mike
Mike

Reputation: 4091

It looks like your variables are mismatched. You are setting username but getting Username. In JavaScript, variables are case sensitive so username will not always equal Username, they are two different variables.

Using your functions:

function writeLocalStorage() {
  localStorage.username = 'mike';
}

function getLocalStorage() {
  console.log(localStorage.Username); // what you currently have, will return undefined
  console.log(localStorage.username); // returns "mike"
}

// returns:
//   undefined
//   mike
getLocalStorage();

Upvotes: 3

Related Questions