Skip to content

Instantly share code, notes, and snippets.

@jessemlay
Last active July 3, 2019 21:47
Show Gist options
  • Save jessemlay/6416bc19fcde6f7f15d8604ddcb0bec1 to your computer and use it in GitHub Desktop.
Save jessemlay/6416bc19fcde6f7f15d8604ddcb0bec1 to your computer and use it in GitHub Desktop.
// form.js
const formId = "save-later-form"; // ID of the form
const url = location.href; // href for the page
const formIdentifier = `${url} ${formId}`; // Identifier used to identify the form
const saveButton = document.querySelector("#save"); // select save button
const alertBox = document.querySelector(".alert"); // select alert display div
let form = document.querySelector(`#${formId}`); // select form
let formElements = form.elements; // get the elements in the form
/**
* This function gets the values in the form
* and returns them as an object with the
* [formIdentifier] as the object key
* @returns {Object}
*/
const getFormData = () => {
let data = { [formIdentifier]: {} };
for (const element of formElements) {
if (element.name.length > 0) {
data[formIdentifier][element.name] = element.value;
}
}
return data;
};
saveButton.onclick = event => {
event.preventDefault();
data = getFormData();
localStorage.setItem(formIdentifier, JSON.stringify(data[formIdentifier]));
const message = "Form draft has been saved!";
displayAlert(message);
};
/**
* This function displays a message
* on the page for 1 second
*
* @param {String} message
*/
const displayAlert = message => {
alertBox.innerText = message;
alertBox.style.display = "block";
setTimeout(function() {
alertBox.style.display = "none";
}, 1000);
};
/**
* This function populates the form
* with data from localStorage
*
*/
const populateForm = () => {
if (localStorage.key(formIdentifier)) {
const savedData = JSON.parse(localStorage.getItem(formIdentifier)); // get and parse the saved data from localStorage
for (const element of formElements) {
if (element.name in savedData) {
element.value = savedData[element.name];
}
}
const message = "Form has been refilled with saved data!";
displayAlert(message);
}
};
document.onload = populateForm();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment