Created
October 31, 2021 10:03
-
-
Save GeeWee/2da809b8db6872c25ecaf6d0dd92d448 to your computer and use it in GitHub Desktop.
form.js
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
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