diff --git a/Sprint-3/todo-list/index.html b/Sprint-3/todo-list/index.html index ee3ef64fd..a3f130b66 100644 --- a/Sprint-3/todo-list/index.html +++ b/Sprint-3/todo-list/index.html @@ -1,27 +1,29 @@ - - - - Title here - - - -
-
- -
-
- - -
-
- - - + + + + + Title here + + + + +
+
+ +
+
+ + +
+
+ + + + + + \ No newline at end of file diff --git a/Sprint-3/todo-list/script.js b/Sprint-3/todo-list/script.js index 61982a54f..83d1c13a4 100644 --- a/Sprint-3/todo-list/script.js +++ b/Sprint-3/todo-list/script.js @@ -1,5 +1,18 @@ +const addNewTodoButton = document.getElementById("add-new-todo"), + removeAllCompletedButton = document.getElementById("remove-all-completed"), + inputDom = document.getElementById("task-input"), + listDom = document.getElementById("todo-list"); + +addNewTodoButton.addEventListener("click", function (event) { + addNewTodo(event); + inputDom.value = ''; +}); + function populateTodoList(todos) { - let list = document.getElementById("todo-list"); + for (const todo of todos) { + createNewTodoDom(todo); + } + // Write your code to create todo list elements with completed and delete buttons here, all todos should display inside the "todo-list" element. } @@ -17,9 +30,48 @@ function addNewTodo(event) { // The code below prevents the page from refreshing when we click the 'Add Todo' button. event.preventDefault(); // Write your code here... and remember to reset the input field to be blank after creating a todo! + const todo = { task: inputDom.value, completed: false }; + todos.push(todo); + createNewTodoDom(todo); } +function createNewTodoDom(todo) { + const todoLiDom = document.createElement("li"), + entryTextDom = document.createElement("p"); + if (todo.completed) { + todoLiDom.setAttribute("class", "completed"); + } + todoLiDom.appendChild(entryTextDom); + entryTextDom.textContent = todo.task; + listDom.appendChild(todoLiDom); + + const completeButton = document.createElement("button"); + completeButton.setAttribute("class", "completeBtn"); + completeButton.textContent = "Complete"; + const deleteButton = document.createElement("button"); + deleteButton.setAttribute("class", "deleteBtn"); + deleteButton.textContent = "Delete"; + todoLiDom.appendChild(completeButton); + todoLiDom.appendChild(deleteButton); + + completeButton.addEventListener("click", () => { + entryTextDom.style.textDecoration = 'line-through'; + todoLiDom.setAttribute("class", "completed") + }); + deleteButton.addEventListener("click", () => { + todoLiDom.remove(); + }); +} + +removeAllCompletedButton.addEventListener("click", () => { + deleteAllCompletedTodos(); +}); + // Advanced challenge: Write a fucntion that checks the todos in the todo list and deletes the completed ones (we can check which ones are completed by seeing if they have the line-through styling applied or not). function deleteAllCompletedTodos() { - // Write your code here... -} + for (let i = listDom.children.length - 1; i >= 0; i--) { + if (listDom.children[i].classList.contains("completed")) { + listDom.children[i].remove() + } + } +} \ No newline at end of file