-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (47 loc) · 1.51 KB
/
app.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
// Selectors
document.querySelector('form').addEventListener('submit', handleSubmitForm);
// Event Handlers
function handleSubmitForm(e) {
e.preventDefault();
let input = document.querySelector('input');
if (input.value != '')
addTodo(input.value);
input.value = '';
}
// Helpers
function addTodo(todo) {
let ul = document.querySelector('ul');
let li = document.createElement('li');
li.innerHTML = `
<span class="todo-item">${todo}</span>
<button name="checkButton"><i class="fas fa-check-square"></i></button>
<button name="deleteButton" ><i class="fas fa-trash"></i></button>
`;
li.classList.add('todo-list-item');
ul.appendChild(li);
}
document.querySelector('ul').addEventListener('click', handleClickDeleteOrCheck);
function handleClickDeleteOrCheck(e) {
if (e.target.name == 'checkButton')
checkTodo(e);
if (e.target.name == 'deleteButton')
deleteTodo(e);
}
function checkTodo(e) {
let item = e.target.parentNode;
if (item.style.textDecoration == 'line-through')
item.style.textDecoration = 'none';
else
item.style.textDecoration = 'line-through';
}
function deleteTodo(e) {
let item = e.target.parentNode;
item.addEventListener('transitionend', function () {
item.remove();
});
item.classList.add('todo-list-item-fall');
}
document.getElementById('clearAll').addEventListener('click', handleClearAll);
function handleClearAll(e) {
document.querySelector('ul').innerHTML = '';
}