Skip to content

Instantly share code, notes, and snippets.

@shawnbot
Last active July 29, 2021 14:54
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shawnbot/89855705204494c6d8bf to your computer and use it in GitHub Desktop.
Save shawnbot/89855705204494c6d8bf to your computer and use it in GitHub Desktop.
A JavaScript snippet for temporarily saving and restoring a form's state after refreshing
// NOTE: the '#form-id' selector below will most certainly be different
// for whatever page you're on. Remember to change it in both instances!
// run this in the terminal to save the state of your form
// in your browser's LocalStorage:
[].forEach.call(document.querySelector('#form-id').elements, function(el) {
localStorage.setItem(el.name, el.value);
});
// then refresh the page and run this to restore your form values:
[].forEach.call(document.querySelector('#form-id').elements, function(el) {
el.value = localStorage.getItem(el.name);
});
@gingerbeardman
Copy link

Doesn't work with radio buttons that inherently share the same name. All of those overwrite each other so the last one on the page is the one that is saved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment