Skip to content

Commit e90457f

Browse files
committed
Make event listeners consistent with named functions
1 parent 45f381e commit e90457f

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

‎Sprint-3/todo-list/script.mjs‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,23 @@ import * as Todos from "./todos.mjs";
55
const todos = [];
66

77
// Set up tasks to be performed once on page load
8-
window.addEventListener("load", () => {
8+
function setup() {
99
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);
1010
document
1111
.getElementById("delete-completed-btn")
12-
.addEventListener("click", () => {
13-
const updated = Todos.deleteCompleted(todos);
14-
todos.length = 0;
15-
todos.push(...updated);
16-
render();
17-
});
18-
19-
// Populate sample data
12+
.addEventListener("click", deleteCompletedTodos);
13+
14+
// Populate sample data
2015
Todos.addTask(todos, "Wash the dishes", false);
2116
Todos.addTask(todos, "Do the shopping", true);
2217

2318
render();
24-
});
19+
}
20+
21+
window.addEventListener("load", setup);
2522

2623
// A callback that reads the task description from an input field and
27-
// append a new task to the todo list.
24+
// appends a new task to the todo list.
2825
function addNewTodo() {
2926
const taskInput = document.getElementById("new-task-input");
3027
const task = taskInput.value.trim();
@@ -36,6 +33,15 @@ function addNewTodo() {
3633
taskInput.value = "";
3734
}
3835

36+
// Delete all completed todo tasks
37+
function deleteCompletedTodos() {
38+
const updated = Todos.deleteCompleted(todos);
39+
todos.length = 0;
40+
todos.push(...updated);
41+
render();
42+
43+
}
44+
3945
// Note:
4046
// - Store the reference to the <ul> element with id "todo-list" here
4147
// to avoid querying the DOM repeatedly inside render().

0 commit comments

Comments
 (0)