-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
108 lines (97 loc) · 3.17 KB
/
script.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
const list = [];
const button = document.getElementById("enter");
const input = document.getElementById("userInput");
const ul = document.querySelector("ul");
const lis = document.getElementsByTagName("li");
const printButton = document.getElementById("print");
const deleteButton = document.getElementById("delete");
const tooltip = document.getElementById("tooltip");
// empty the input field
const resetInput = () => {
input.value = "";
};
// returns input lenght to check if the item should be send (>1)
const inputLength = () => {
return input.value.length;
};
//Checks if item is already on the list
const checkItemDouble = (input) => {
if (list.includes(input) === true) {
alert("This item is already in your shopping list !");
resetInput();
return true;
} else {
return false;
}
};
// add input value as list item after "add an item" button is pressed
const addListAfterClick = () => {
if (checkItemDouble(input.value) === false && inputLength() > 0) {
createListElement(input.value);
}
};
// add input value as list item after enter key is pressed
const addListAfterKeypress = (event) => {
if (
inputLength() > 0 &&
event.key === "Enter" &&
checkItemDouble(input.value) === false
) {
createListElement(input.value);
}
};
// change list style and icons to done
function crossedList() {
this.classList.toggle("done");
}
//delete individual list item
function deleteItem() {
this.parentNode.remove();
}
// create new element with input value, and trash button, and the crossed class
const createListElement = (value) => {
const li = document.createElement("li");
li.appendChild(document.createTextNode(value));
li.addEventListener("click", crossedList);
ul.appendChild(li);
list.push(value);
const trashButton = document.createElement("button");
trashButton.classList.add("trash");
li.appendChild(trashButton);
const trash = document.createElement("i");
trash.classList.add("fa");
trash.classList.add("fa-trash");
trashButton.appendChild(trash);
trashButton.addEventListener("click", deleteItem);
resetInput();
};
//print the page and hide/show buttons
const printPage = () => {
tooltip.classList.add("hidden");
deleteButton.classList.add("hidden");
button.classList.add("hidden");
input.classList.add("hidden");
printButton.classList.add("hidden");
window.print();
deleteButton.classList.remove("hidden");
button.classList.remove("hidden");
input.classList.remove("hidden");
printButton.classList.remove("hidden");
tooltip.classList.remove("hidden");
};
//delete the entire list
const deleteList = () => {
const elements = document.querySelectorAll("li");
for (var i = 0; i < elements.length; i++) {
elements[i].remove();
}
};
// create "placeholder" items
const cart = ["Milk", "Water", "Eggs", "Oranges", "Lemons"];
for (var i = 0; i < cart.length; i++) {
createListElement(cart[i]);
}
deleteButton.addEventListener("click", deleteList);
printButton.addEventListener("click", printPage);
button.addEventListener("click", addListAfterClick);
input.addEventListener("keypress", addListAfterKeypress);