How to save data from a form with HTML5 Local Storage?

LocalStorage has a setItem method. You can use it like this:

var inputEmail= document.getElementById("email");
localStorage.setItem("email", inputEmail.value);

When you want to get the value, you can do the following:

var storedValue = localStorage.getItem("email");

It is also possible to store the values on button click, like so:

<button onclick="store()" type="button">StoreEmail</button>

<script  type="text/javascript">
  function store(){
     var inputEmail= document.getElementById("email");
     localStorage.setItem("email", inputEmail.value);
    }
</script>

Here's a quick function that will store the value of an <input>, <textarea> etc in local storage, and restore it on page load.

function persistInput(input)
{
  var key = "input-" + input.id;

  var storedValue = localStorage.getItem(key);

  if (storedValue)
      input.value = storedValue;

  input.addEventListener('input', function ()
  {
      localStorage.setItem(key, input.value);
  });
}

Your input element must have an id specified that is unique amongst all usages of this function. It is this id that identifies the value in local storage.

var inputElement = document.getElementById("name");

persistInput(inputElement);

Note that this method adds an event handler that is never removed. In most cases that won't be a problem, but you should consider whether it would be in your scenario.


Here,Simple solution using JQUERY is like this..

var username = $('#username').val();
var password = $('#password').val();
localStorage.setItem("username", username);
localStorage.setItem("password", password);

You can save form data in localstorage applying form validation using javascript. Just read this easy article https://xeeratech.com/form-validation-in-javascript-and-store-form-data-in-localstorage/