-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
100 lines (64 loc) · 2.08 KB
/
Copy pathapp.js
File metadata and controls
100 lines (64 loc) · 2.08 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
let newTodo = document.querySelector('#newTodo');
let list = document.querySelector('.list');
let addTodoBtn = document.querySelector('.addTodoBtn');
let resetBtn = document.querySelector('.resetBtn')
let removeBtn = document.querySelector('.removeBtn')
//Add a todo to the list of items
addTodoBtn.addEventListener('click', insert)
// newTodo.addEventListener('change', addTodo) //type enter to add the todo
//reset the list when we click on the reset button
resetBtn.addEventListener('click', resetList)
//Function to reset the list
function resetList () {
let divs = document.querySelectorAll('.item') //select all items block
divs.forEach(div => {
div.remove()
});
}
//Function to add a todo to the list
function insert() {
let newDiv = document.createElement('div');
let newCheckBox = document.createElement('input');
newDiv.classList.add("py-1")
newCheckBox.type = "checkbox"
newCheckBox.classList.add("box")
newCheckBox.classList.add("form-check-input")
newDiv.classList.add("item")
let todo = newTodo.value;
console.log(todo)
if (todo) {
list.append(newDiv);
newDiv.append(newCheckBox);
newDiv.append(todo);
} else {
alert("Enter a to-do")
}
clear() //clear the input
removeBtn.addEventListener('click', remove) //remove selected todo(s)
newCheckBox.addEventListener('click', cancel) //cross out selected todo(s)
}
//Function to remove checked items
function remove() {
let boxes = document.querySelectorAll('.box')
boxes.forEach(box => {
if (box.checked) {
box.parentElement.remove()
}
});
}
//Function to clear the input field
function clear() {
newTodo.value = ""
}
//Function to cancel a todo
function cancel() {
let boxes = document.querySelectorAll('.box')
boxes.forEach(box => {
let parentBox = box.parentElement
if (box.checked) {
parentBox.style.textDecorationLine = "line-through"
} else {
parentBox.style.textDecorationLine = "none"
}
});
}