-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay22_GiftApp.js
68 lines (48 loc) · 1.4 KB
/
Day22_GiftApp.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
let people = []
const inputFieldEl = document.getElementById("input-field")
const addButtonEl = document.getElementById("add-button")
const peopleListEl = document.getElementById("people-list")
addButtonEl.addEventListener("click", function() {
let inputValue = inputFieldEl.value
if (inputValue) {
people.push(inputValue)
clearInputFieldEl()
renderList(people)
}
saveData(inputValue) //here
})
function renderList(array) {
clearPeopleListEl()
for (let i = 0; i < array.length; i++) {
let currentPerson = array[i]
appendPersonToPeopleListEl(currentPerson)
}
}
function clearPeopleListEl() {
peopleListEl.innerHTML = ""
}
function clearInputFieldEl() {
inputFieldEl.value = ""
}
function appendPersonToPeopleListEl(person) {
let newEl = document.createElement("li")
newEl.textContent = person
newEl.addEventListener("dblclick", function() {
let index = people.indexOf(person)
people.splice(index, 1)
renderList(people)
})
peopleListEl.append(newEl)
}
//new
function saveData(person) {
localStorage.setItem('people', JSON.stringify(people))
}
function loadData() {
const storedData = localStorage.getItem("people")
if (storedData) {
people = JSON.parse(storedData)
renderList(people)
}
}
loadData()