1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
const form = document.querySelector('#save-me'); let inputs = Array.prototype.slice.call(document.querySelectorAll('[data-type="input"]')); function saveInputValue (e) { if (e.target.length < 0 ) return; localStorage.setItem(`form-${e.target.id}`, e.target.value); } function getInputsFromLocalStorage () { inputs.forEach(function (input){ input.value = localStorage.getItem(`form-${input.id}`); }) } function clearDataOnSubmit () { localStorage.clear(); } form.addEventListener('input', saveInputValue); form.addEventListener('submit', clearDataOnSubmit); getInputsFromLocalStorage ();
|