-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
159 lines (128 loc) · 4.83 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//To-do
const addToDoButton = document.getElementById('addToDo');
const toDoContainer = document.getElementById('toDoContainer');
const inputField = document.getElementById('inputField');
const clearAllButton = document.getElementById('clearAll')
const tasks = JSON.parse(localStorage.getItem("tasks")) || [];
const addTask = (task) => {
tasks.push({task})
localStorage.setItem("tasks", JSON.stringify(tasks));
return {task};
}
const createTask = ({task}) => {
task = task.trim();
if (task != ''){
let paragraph = document.createElement('p');
let button = document.createElement('button');
paragraph.classList.add('paragraph-styling');
button.innerHTML = 'delete'
paragraph.innerHTML = task;
paragraph.append(button);
toDoContainer.appendChild(paragraph);
button.addEventListener('click', function(){
toDoContainer.removeChild(paragraph);
let index = tasks.findIndex( (element) => element.task === task);
tasks.splice(index, 1);
localStorage.setItem("tasks", JSON.stringify(tasks));
})
clearAllButton.addEventListener('click',function(){
toDoContainer.remove(paragraph);
})
paragraph.addEventListener('click', function(){
if (paragraph.style.textDecoration == "line-through"){
paragraph.style.textDecoration = "none";
} else{
paragraph.style.textDecoration = "line-through";
}
})
}
}
tasks.forEach(createTask);
//add Task with enter
inputField.addEventListener("keypress", function(event) {
if (event.key === "Enter") {
event.preventDefault();
addToDoButton.click();
}
});
addToDoButton.addEventListener('click',function(){
const newtask = addTask(
inputField.value
)
createTask(newtask);
inputField.value = "";
})
//saved
const addSavedButton = document.getElementById('addSaved');
const savedContainer = document.getElementById('savedContainer');
const inputFieldSaved = document.getElementById('inputFieldSaved');
const saved = JSON.parse(localStorage.getItem("saved")) || [];
const addSave = (task) => {
saved.push({task})
localStorage.setItem("saved", JSON.stringify(saved));
return {task};
}
const createSave = ({task}) => {
task = task.trim();
if (task != ''){
let paragraph = document.createElement('p');
let button = document.createElement('button');
paragraph.classList.add('paragraph-styling');
button.innerHTML = 'delete'
paragraph.innerHTML = task;
paragraph.append(button);
savedContainer.appendChild(paragraph);
button.addEventListener('click', function(e){
e.stopPropagation();
savedContainer.removeChild(paragraph);
let index = saved.findIndex( (element) => element.task === task);
saved.splice(index, 1);
localStorage.setItem("saved", JSON.stringify(saved));
})
paragraph.addEventListener('click', function(){
const paragraph2 = document.createElement('p');
const button2 = document.createElement('button');
paragraph2.classList.add('paragraph-styling');
button2.innerHTML = 'delete'
paragraph2.innerHTML = task;
paragraph2.append(button2);
toDoContainer.appendChild(paragraph2);
button2.addEventListener('click', function(){
toDoContainer.removeChild(paragraph2);
let index = tasks.findIndex( (element) => element.task === task);
tasks.splice(index, 1);
localStorage.setItem("tasks", JSON.stringify(tasks));
})
paragraph2.addEventListener('click', function(){
if (paragraph2.style.textDecoration == "line-through"){
paragraph2.style.textDecoration = "none";
} else{
paragraph2.style.textDecoration = "line-through";
}
})
// add to tasks object
tasks.push({task})
localStorage.setItem("tasks", JSON.stringify(tasks));
return {task};
})
}
}
saved.forEach(createSave);
inputFieldSaved.addEventListener("keypress", function(event) {
if (event.key === "Enter") {
event.preventDefault();
addSavedButton.click();
}
});
addSavedButton.addEventListener('click',function(){
const newsave = addSave(
inputFieldSaved.value
)
createSave(newsave);
inputFieldSaved.value = "";
})
//clear all button
clearAllButton.addEventListener('click',function(){
localStorage.clear();
location.reload();
})