-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
54 lines (45 loc) · 1.01 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
;(function() {
'use strict'
//
// Variables
//
const form = document.querySelector('#save-me')
//
// Methods
//
/**
* Handle input events
* @param {Event} event The event object
*/
const inputHandler = evt => {
if (!event.target.closest('#save-me')) return
localStorage.setItem(evt.target['name'], evt.target.value)
}
/**
* Handle submit events
* @param {Event} event The event object
*/
const submitHandler = evt => {
if (!evt.target.closest('form').matches('#save-me')) return
localStorage.clear()
}
/**
* Load saved form data from localStorage
*/
const populateForm = _ => {
for (let item in localStorage) {
if (localStorage.hasOwnProperty(item)) {
form[item].value = localStorage.getItem(item)
}
}
}
//
// Inits & Event Listeners
//
// Load saved data from storage
populateForm()
// Listen for input events
document.addEventListener('input', inputHandler, false)
// Listen for submit events
document.addEventListener('submit', submitHandler, false)
})()