-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodoList.html
More file actions
147 lines (113 loc) · 3.83 KB
/
todoList.html
File metadata and controls
147 lines (113 loc) · 3.83 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
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
<!DOCTYPE html>
<html>
<head>
<title>To do list</title>
</head>
<body>
<button id="toggle-all-button">Toggle all</button>
<button id="add-button">Add todo</button>
<input id="add-input">
<ul id="todos-ul">
</ul>
<script>
var todos = [
{todoText: 'Item 1', completed: false},
{todoText: 'Item 2', completed: false},
{todoText: 'Item 3', completed: false}
];
displayTodos();
function add(){
var initialTodoText = addInput.value;
todos.push({todoText: initialTodoText, completed: false});
addInput.value = '';
displayTodos();
}
function edit(event) {
var result = prompt('Edit your todo');
var position = event.currentTarget.id.split('-')[1];
if (result === null || result === '') {
// do nothing
}
else {
var newTodoText = result;
todos[position].todoText = newTodoText;
}
displayTodos();
}
function remove(event) {
var position = event.currentTarget.id.split('-')[1];
todos.splice(position, 1);
displayTodos();
}
function toggle(event){
var position = event.currentTarget.id.split('-')[1];
if (todos[position].completed === false) {
todos[position].completed = true;
}
else {
todos[position].completed = false;
}
displayTodos();
}
function displayTodos() {
var todoListUl = document.getElementById('todos-ul');
todoListUl.innerHTML = '';
for (var i = 0; i < todos.length; i++) {
var todoLi = document.createElement('li');
var completedStatus = '[X]';
if (todos[i].completed === true) {
completedStatus = '[X] ';
}
else {
completedStatus = '[ ] ';
}
todoLi.innerHTML = completedStatus + todos[i].todoText;
todoListUl.appendChild(todoLi);
var removeButton = document.createElement('button');
removeButton.innerText = 'Remove';
todoLi.appendChild(removeButton);
removeButton.addEventListener('click', remove);
removeButton.id = 'remove-' + i;
var toggleButton = document.createElement('button');
toggleButton.innerText = 'Toggle';
todoLi.appendChild(toggleButton);
toggleButton.addEventListener('click', toggle);
toggleButton.id = 'toggle-'+i;
var editButton = document.createElement('button');
editButton.innerText = 'Edit';
todoLi.appendChild(editButton);
editButton.addEventListener('click', edit);
editButton.id = 'edit-' + i;
}
}
function toggleAll() {
var completedTodos = 0;
for (var i = 0; i < todos.length; i++){
if (todos[i].completed === true) {
completedTodos++;
}
}
if (completedTodos === todos.length){
for (var i = 0; i < todos.length; i++){
todos[i].completed = false;
}
}
else {
for (var i = 0; i < todos.length; i++){
todos[i].completed = true;
}
}
displayTodos();
}
var toggleAllButton = document.getElementById('toggle-all-button');
toggleAllButton.addEventListener('click', toggleAll);
var addButton = document.getElementById('add-button');
var addInput = document.getElementById('add-input');
addButton.addEventListener('click', add);
var editPositionInput = document.getElementById('edit-position-input');
var editTextInput = document.getElementById('edit-text-input');
var editButton = document.getElementById('edit-button');
editButton.addEventListener('click', edit);
</script>
</body>
</html>