Skip to content

Instantly share code, notes, and snippets.

@GeeWee
Created October 31, 2021 10:03
Show Gist options
  • Save GeeWee/2da809b8db6872c25ecaf6d0dd92d448 to your computer and use it in GitHub Desktop.
Save GeeWee/2da809b8db6872c25ecaf6d0dd92d448 to your computer and use it in GitHub Desktop.
form.js
function doWork() {
const candidateForm = document.querySelector("#candidate-form")
candidateForm.addEventListener("submit", (e) => {
console.log("form submitted");
const formData = new FormData(document.querySelector('form'))
for (var pair of formData.entries()) {
console.log(pair[0] + ': ' + pair[1]);
localStorage.setItem(pair[0], pair[1])
}
})
function simulateChange(element) {
var ev = new Event('change', { bubbles: true }), value; {
}
element.dispatchEvent(ev)
}
function setElementValue(element, value) {
if (value) {
element.value = value
simulateChange(element)
}
}
function assignSavedProperties() {
const firstNameElement = document.querySelector('input[name="firstName"]');
setElementValue(firstNameElement, localStorage.getItem("firstName"))
const lastNameElement = document.querySelector('input[name="lastName"]');
setElementValue(lastNameElement, localStorage.getItem("lastName"))
const emailElement = document.querySelector('input[name="email"]');
setElementValue(emailElement, localStorage.getItem("email"))
const phoneElement = document.querySelector('input[name="phone"]');
setElementValue(phoneElement, localStorage.getItem("phone"))
if (localStorage.getItem("personopplysninger") != null) {
const personoplysningerElement = document.querySelector('input[name="personopplysninger"]').checked = true;
}
}
assignSavedProperties();
}
if (document.readyState == 'loading') {
// still loading, wait for the event
document.addEventListener('DOMContentLoaded', doWork);
} else {
// DOM is ready!
doWork();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment