-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
134 lines (116 loc) · 4.26 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const myShoppingList = () => {
const button = document.getElementById('enter');
const input = document.getElementById('userinput');
const ul = document.querySelector('ul');
const btnClear = document.getElementById('clear');
const listItems = document.querySelectorAll('#myList li');
const search = document.getElementById('userSearch');
const btnSearch = document.getElementById('search');
const inputLength = () => {
return input.value.length;
};
const createListElement = () => {
let createLi = document.createElement('li');
let capitalizedValue =
input.value.charAt(0).toUpperCase() + input.value.slice(1);
createLi.appendChild(document.createTextNode(capitalizedValue));
addDeleteButton(createLi);
ul.appendChild(createLi);
input.value = '';
updatedList();
};
const addListAfterClick = () => {
if (inputLength() > 0) {
createListElement();
}
};
const addListAfterKeypress = (event) => {
if (inputLength() > 0 && event.keyCode === 13) {
createListElement();
}
};
const checkListFromItem = () => {
const listItems = document.querySelectorAll('#myList li'); //update listItems
checkList(listItems, search.value);
};
const clearList = () => {
while (ul.firstChild) {
ul.removeChild(ul.firstChild);
}
};
const checkList = (list, lookingFor) => {
for (let i = 0; i < list.length; i++) {
let itemText = list[i].firstChild.textContent;
if (itemText.toLowerCase() === lookingFor.toLowerCase()) {
document.querySelector(
'#searchResult'
).textContent = `${lookingFor} is in your list`;
return;
}
}
document.querySelector(
'#searchResult'
).textContent = `${lookingFor} does not exist in your list`;
};
button.addEventListener('click', addListAfterClick);
btnClear.addEventListener('click', clearList);
input.addEventListener('keypress', addListAfterKeypress);
btnSearch.addEventListener('click', checkListFromItem);
const addDeleteButton = (parent) => {
let button = parent.appendChild(document.createElement('button'));
button.appendChild(document.createTextNode('delete'));
button.id = 'btn_delete';
button.onclick = removeList;
};
//function to add the 'line through' style
const toggleList = (element) => {
element.classList.toggle('done');
};
const removeList = (event) => {
if (event.target.tagName === 'BUTTON') {
event.target.parentElement.remove();
updatedList();
} else {
console.error('Error');
}
};
listItems.forEach((element) => {
addDeleteButton(element);
element.addEventListener('click', () => {
if (element !== undefined) {
toggleList(element);
}
});
});
const updatedList = () => {
const listItems = document.querySelectorAll('#myList li');
const listValues = Array.from(listItems).map(
(item) => item.firstChild.textContent
);
localStorage.setItem('myList', JSON.stringify(listValues));
};
const loadListValue = () => {
const savedListValue = localStorage.getItem('myList');
if (savedListValue) {
const parsedListValue = JSON.parse(savedListValue);
const list = document.querySelector('#myList');
while (list.firstChild) {
list.removeChild(list.firstChild);
}
parsedListValue.forEach((value) => {
let createLi = document.createElement('li');
createLi.appendChild(document.createTextNode(value));
addDeleteButton(createLi);
list.appendChild(createLi);
});
}
};
const loadButton = document.querySelector('#load_button');
loadButton.onclick = loadListValue;
loadButton.addEventListener('click', () => {
const listValue = document.querySelector('#myList').value;
updatedList(listValue);
});
window.onload = loadListValue;
};
myShoppingList();