-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (58 loc) · 1.75 KB
/
script.js
File metadata and controls
66 lines (58 loc) · 1.75 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
const toDoItems = [];
class ToDo {
constructor(description) {
this.description = description;
this.complete = false;
}
}
ToDo.prototype.completeToDo = function() {
this.complete = true;
}
function buildToDo(todo, index) {
const newDiv = document.createElement('toDoShell');
newDiv.classList.add('toDoShell');
const newSpan = document.createElement('toDoText');
newSpan.innerHTML = todo.description;
newSpan.id = index;
newSpan.addEventListener('click', completeToDo);
const checkbox = document.createElement('input');
checkbox.setAttribute('type', 'checkbox');
checkbox.id = index;
newSpan.removeAttribute('id');
checkbox.addEventListener('click', completeToDo);
newSpan.removeEventListener('click', completeToDo);
checkbox.classList.add('completeCheckbox');
if (todo.complete) {
newSpan.classList.add('completeText');
checkbox.checked = true;
}
newDiv.appendChild(checkbox);
newDiv.appendChild(newSpan);
return newDiv;
}
function buildToDos(toDos) {
return toDos.map(buildToDo);
}
function displayToDos() {
const toDoContainer = document.querySelector('#toDoContainer');
toDoContainer.innerHTML = '';
buildToDos(toDoItems).forEach(function(toDo) {
toDoContainer.appendChild(toDo);
});
}
function addToDo() {
const newToDo = new ToDo(taskInput.value);
newToDo.value = newToDo;
toDoItems.push(newToDo);
newToDo.value = '';
displayToDos();
}
const addButton = document.querySelector('#addButton');
addButton.addEventListener('click', addToDo);
function completeToDo(event) {
// UNCOMMENT THE NEXT LINE
const index = event.target.id;
toDoItems[index].completeToDo();
displayToDos();
}
displayToDos();