Skip to content

Commit 5aabc3b

Browse files
committed
[day 6] Delegate event listener to the list
1 parent d393574 commit 5aabc3b

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

projects/6-to-do-list/js/to-do.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var form = document.getElementById('todo-form');
22
var toDoList = document.getElementById('todo-items');
33
var newToDo = document.getElementById('todo-add-new');
4-
var toDos = document.querySelectorAll('li.todo__item');
54

65
var addToDo = function() {
76
var txt = newToDo.value;
@@ -19,10 +18,13 @@ var addToDo = function() {
1918
return false;
2019
};
2120

22-
var completeToDo = function() {
23-
this.classList.toggle("todo__item--complete");
24-
contents = toDoList.innerHTML;
25-
localStorage.setItem('contents', contents);
21+
var completeToDo = function(event) {
22+
var toDo = event.target;
23+
if (toDo && toDo.matches("li.todo__item")) {
24+
toDo.classList.toggle("todo__item--complete");
25+
contents = toDoList.innerHTML;
26+
localStorage.setItem('contents', contents);
27+
}
2628
};
2729

2830

@@ -58,13 +60,9 @@ if(localStorage.getItem('contents')) {
5860
}
5961

6062
var toDoHandlers = function() {
61-
var toDos = document.querySelectorAll('li.todo__item');
62-
63-
for(var i = 0; i < toDos.length; i++) {
64-
toDos[i].addEventListener('click', completeToDo, false);
65-
}
63+
toDoList.addEventListener('click', completeToDo, false);
6664
};
6765

6866
window.onload = function(){
6967
toDoHandlers();
70-
}
68+
};

0 commit comments

Comments
 (0)