Last active
July 29, 2021 14:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.