Forked from CezaryDanielNowak/gist:85302c1d3323e951e3885bcedaa7c10b
Last active
October 17, 2024 15:49
-
-
Save feimosi/f8704e33187b899860bd9b8061e591b2 to your computer and use it in GitHub Desktop.
Persist / restore form data using localStorage
This file contains hidden or 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
// 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