-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
114 lines (99 loc) · 3.59 KB
/
main.js
File metadata and controls
114 lines (99 loc) · 3.59 KB
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
const alert = document.getElementById('alert');
const toDoInput = document.getElementById('toDoInput');
const addButton = document.getElementById('addButton');
const toDos = document.getElementById('toDos');
const emptyAllButton = document.getElementById('emptyAllButton');
/* Function for alert message that appends a p-tag in
the alert div in DOM */
function alertMessage() {
const alertParagraph = document.createElement('p');
alertParagraph.innerHTML =
"<span class='boldText'>Ops!</span> You forgot to add a new to do!";
alert.appendChild(alertParagraph);
/* Prevents that more than one alert paragraph is printed
if addButton is clicked */
addButton.addEventListener('click', function () {
alertParagraph.remove();
})
/* Prevents that more than one alert paragraph is printed
when enter key is pressed or if a new to do is being written */
toDoInput.addEventListener('keypress', function (e) {
const enterKey = e.keyCode;
if (enterKey === 13 && toDoInput.value === '') {
alertParagraph.remove();
} else {
alertParagraph.classList.add('fadeOut');
setTimeout(function () {
alertParagraph.remove();
}, 200);
}
})
// Removes alert paragraph if emptyAllButton is clicked
emptyAllButton.addEventListener('click', function () {
alertParagraph.remove();
})
}
/* Function for creating a new list item with done and
delete buttons */
function createNewToDo() {
const listItem = document.createElement('li');
const buttonContainer = document.createElement('div');
const doneButton = document.createElement('button');
const deleteButton = document.createElement('button');
listItem.innerText = toDoInput.value;
buttonContainer.className = "buttonContainer";
doneButton.className = "doneButton";
doneButton.innerText = "Done";
deleteButton.className = "deleteButton";
deleteButton.innerText = "Delete";
toDos.appendChild(listItem);
listItem.appendChild(buttonContainer);
buttonContainer.appendChild(doneButton);
buttonContainer.appendChild(deleteButton);
document.getElementById('toDoInput').value = '';
/* Moves completed to do to 'done deal' list and
removes 'done' button */
doneButton.addEventListener('click', function () {
buttonContainer.className = "deleteButtonContainer";
doneToDo.appendChild(listItem);
doneButton.remove();
})
function removeItems() {
listItem.classList.add('fadeOut');
setTimeout(function () {
listItem.remove();
}, 200);
}
// Deletes todo from 'stuff to do' list when clicked
deleteButton.addEventListener('click', function () {
removeItems();
})
// Deletes every to do on both lists
emptyAllButton.addEventListener('click', function () {
removeItems();
})
}
/* On enter key, event creates a new to do
and if empty input field – an alert is sent */
toDoInput.addEventListener('keypress', function (e) {
const enterKey = e.keyCode;
if (enterKey === 13) {
if (toDoInput.value === '') {
alertMessage();
} else {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById('addButton').click();
}
}
}
})
/* On click, event creates li and button elements
and if empty input field – an alert is sent */
addButton.addEventListener('click', function () {
if (toDoInput.value === '') {
alertMessage();
} else {
createNewToDo();
}
})