0

I need to get data from an HTML form and store them at localStore, but I'm running into some problems while attempting to tho this.

Let's make a short example:

<form class="w3-container" id="form"> 
<p><label>Name</label><input class="w3input" type="text" id="name"></p>
<p><label>Adress</label><input class="w3-input" type="text" id="adress"></p>
<p><label>Number</label><input class="w3-input" type="text" id="number"></p>
<button class="w3-button w3-blue">Submit</button>
</form>

Then JavaScript code:

localStorage.setItem("name", document.getElementById("name").value);
localStorage.setItem("adress", document.getElementById("adress").value);
localStorage.setItem("number", document.getElementById("number").value);  

I have tried a different number of ways to do this but nothing seems to work. What am I missing?

1
  • Code seems incomplete. If you are storing the data in localStorage, you need to have onclick attribute on the Submit button. Commented Jan 19, 2019 at 2:18

3 Answers 3

1

If you're trying to store the data in localStorage when Submit button is clicked, your code should be as follows.

function onSubmit() {
  localStorage.setItem("name", document.getElementById("name").value);
  localStorage.setItem("adress", document.getElementById("adress").value);
  localStorage.setItem("number", document.getElementById("number").value);
}
<form class="w3-container" id="form">
  <p><label>Name</label><input class="w3input" type="text" id="name"></p>
  <p><label>Adress</label><input class="w3-input" type="text" id="adress"></p>
  <p><label>Number</label><input class="w3-input" type="text" id="number"></p>
  <button class="w3-button w3-blue" onclick="onSubmit()">Submit</button>
</form>

Sign up to request clarification or add additional context in comments.

2 Comments

I believe you should avoid using inline javascript (unless you're using a framework e.g react). you should bind the form element to a variable in an external javascript file (or within script tags) then, listen for the submit event on the form element, then grab and save the values you want.
Glad for the knowledge share. I've seen this post which was posted as an answer from other user, better explains the difference between usage of onclick and addEventListener. @Oguntoye
1

Within <script></script> tags or an external js file,

  1. Get the form element: const form = document.getElementById('form');

  2. Add a listener to listen for the submit event and save your values:

    form.addEventListener('submit', () => {
      localStorage.setItem("name", document.getElementById("name").value);
      localStorage.setItem("adress", document.getElementById("adress").value);
      localStorage.setItem("number", document.getElementById("number").value);
    })
    

You should avoid using inline javascript (unless you're using a framework e.g react (then it's not even really inline)).

Comments

0

Alternatively you could add an Event Listener.
addEventListener vs onclick

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.