-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
73 lines (57 loc) · 1.97 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
const formAdd = document.getElementById('form-add')
const resultField = document.getElementById('result-field');
const get = () => {
// Get data from storage, or set empty data
const PEOPLE = window.localStorage.getItem('PEOPLE')
if (PEOPLE) return JSON.parse(PEOPLE)
else return []
}
const set = (newData) => {
window.localStorage.setItem('PEOPLE', JSON.stringify(newData))
}
let PEOPLE = get()
// Form validation [Bootstrap 4]
window.addEventListener('load', function () {
let forms = document.getElementsByClassName('needs-validation');
let validation = Array.prototype.filter.call(forms, function (form) {
form.addEventListener('submit', function (event) {
event.preventDefault()
if (form.checkValidity() === false) {
event.stopPropagation();
} else {
addToStorage()
}
form.classList.add('was-validated');
}, false);
});
}, false);
// Function to call the data from local data storage
const showPeople = () => {
const PEOPLE_STORAGE = window.localStorage.getItem('PEOPLE');
const PEOPLE = JSON.parse(PEOPLE_STORAGE) || [];
resultField.innerHTML = ""
PEOPLE.forEach(person => {
const divChild = document.createElement('div')
let personInfo = '';
for (let key in person) {
personInfo += `${key}: ${person[key]}<br>`
}
divChild.innerHTML = `<b>Person:</b><br> ${personInfo}`
resultField.appendChild(divChild)
})
}
// DOM Function
const addToStorage = () => {
const newPerson = {
firstName: document.getElementById('first-name').value,
lastName: document.getElementById('last-name').value,
address: document.getElementById('address').value,
phoneNumber: document.getElementById('phone-number').value,
email: document.getElementById('email').value
}
PEOPLE.push(newPerson)
set(PEOPLE)
showPeople()
}
showPeople()