Skip to content

Commit 08149c3

Browse files
implemented the deleteCompletedTasks function and added the even listener to the UI
1 parent 1ec4254 commit 08149c3

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Store everything imported from './todos.mjs' module as properties of an object named Todos
1+
// Store everything imported from './todos.mjs' module as properties of an object named Todos
22
import * as Todos from "./todos.mjs";
33

44
// To store the todo tasks
@@ -8,15 +8,18 @@ const todos = [];
88
window.addEventListener("load", () => {
99
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);
1010

11+
document
12+
.getElementById("delete-completed-btn")
13+
.addEventListener("click", deleteCompletedTasks);
14+
1115
// Populate sample data
12-
Todos.addTask(todos, "Wash the dishes", false);
16+
Todos.addTask(todos, "Wash the dishes", false);
1317
Todos.addTask(todos, "Do the shopping", true);
1418

1519
render();
1620
});
1721

18-
19-
// A callback that reads the task description from an input field and
22+
// A callback that reads the task description from an input field and
2023
// append a new task to the todo list.
2124
function addNewTodo() {
2225
const taskInput = document.getElementById("new-task-input");
@@ -28,6 +31,11 @@ function addNewTodo() {
2831

2932
taskInput.value = "";
3033
}
34+
// delete the completed tasks
35+
function deleteCompletedTasks() {
36+
Todos.deleteCompleted(todos);
37+
render();
38+
}
3139

3240
// Note:
3341
// - Store the reference to the <ul> element with id "todo-list" here
@@ -45,12 +53,11 @@ function render() {
4553
});
4654
}
4755

48-
4956
// Note:
5057
// - First child of #todo-item-template is a <li> element.
5158
// We will create each ToDo list item as a clone of this node.
5259
// - This variable is declared here to be close to the only function that uses it.
53-
const todoListItemTemplate =
60+
const todoListItemTemplate =
5461
document.getElementById("todo-item-template").content.firstElementChild;
5562

5663
// Create a <li> element for the given todo task
@@ -62,15 +69,15 @@ function createListItem(todo, index) {
6269
li.classList.add("completed");
6370
}
6471

65-
li.querySelector('.complete-btn').addEventListener("click", () => {
72+
li.querySelector(".complete-btn").addEventListener("click", () => {
6673
Todos.toggleCompletedOnTask(todos, index);
6774
render();
6875
});
69-
70-
li.querySelector('.delete-btn').addEventListener("click", () => {
76+
77+
li.querySelector(".delete-btn").addEventListener("click", () => {
7178
Todos.deleteTask(todos, index);
7279
render();
7380
});
7481

7582
return li;
76-
}
83+
}

0 commit comments

Comments
 (0)