Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save feimosi/f8704e33187b899860bd9b8061e591b2 to your computer and use it in GitHub Desktop.
Save feimosi/f8704e33187b899860bd9b8061e591b2 to your computer and use it in GitHub Desktop.
Persist / restore form data using localStorage
// Persist data:
localStorage._formData_ = JSON.stringify(Array.from(document.forms[0].querySelectorAll('input')).map((el) => el.value));
// Restore data:
_formData_ = JSON.parse(localStorage._formData_) || [];
Array.from(document.forms[0].querySelectorAll('input')).forEach((input, id) => {
input.value = _formData_[id];
var event = document.createEvent("HTMLEvents");
event.initEvent("input", true, true);
input.dispatchEvent(event);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment