|
1 | 1 | function populateTodoList(todos) { |
2 | 2 | let list = document.getElementById("todo-list"); |
3 | | - // Write your code to create todo list elements with completed and delete buttons here, all todos should display inside the "todo-list" element. |
| 3 | + list.innerHTML = ""; |
| 4 | + |
| 5 | + todos.forEach((todo) => { |
| 6 | + let li = document.createElement("li"); |
| 7 | + li.textContent = todo.task; |
| 8 | + li.className = `list-group-item ${todo.completed ? "completed" : ""}`; |
| 9 | + |
| 10 | + let span = document.createElement("span"); |
| 11 | + span.className = "badge bg-light rounded-pill"; |
| 12 | + |
| 13 | + let checkIcon = document.createElement("i"); |
| 14 | + checkIcon.className = "fa fa-check todo-icon"; |
| 15 | + checkIcon.setAttribute("aria-hidden", "true"); |
| 16 | + checkIcon.addEventListener("click", () => { |
| 17 | + todo.completed = !todo.completed; |
| 18 | + li.classList.toggle("completed"); |
| 19 | + }); |
| 20 | + |
| 21 | + let trashIcon = document.createElement("i"); |
| 22 | + trashIcon.className = "fa fa-trash todo-icon"; |
| 23 | + trashIcon.setAttribute("aria-hidden", "true"); |
| 24 | + trashIcon.addEventListener("click", () => { |
| 25 | + const index = todos.indexOf(todo); |
| 26 | + if (index > -1) { |
| 27 | + todos.splice(index, 1); |
| 28 | + } |
| 29 | + populateTodoList(todos); |
| 30 | + }); |
| 31 | + |
| 32 | + span.appendChild(checkIcon); |
| 33 | + span.appendChild(trashIcon); |
| 34 | + |
| 35 | + li.appendChild(span); |
| 36 | + |
| 37 | + list.appendChild(li); |
| 38 | + }); |
4 | 39 | } |
5 | 40 |
|
6 | | -// These are the same todos that currently display in the HTML |
7 | | -// You will want to remove the ones in the current HTML after you have created them using JavaScript |
8 | 41 | let todos = [ |
9 | 42 | { task: "Wash the dishes", completed: false }, |
10 | 43 | { task: "Do the shopping", completed: false }, |
11 | 44 | ]; |
12 | 45 |
|
13 | | -populateTodoList(todos); |
14 | | - |
15 | | -// This function will take the value of the input field and add it as a new todo to the bottom of the todo list. These new todos will need the completed and delete buttons adding like normal. |
16 | 46 | function addNewTodo(event) { |
17 | | - // The code below prevents the page from refreshing when we click the 'Add Todo' button. |
18 | 47 | event.preventDefault(); |
19 | | - // Write your code here... and remember to reset the input field to be blank after creating a todo! |
| 48 | + let input = document.getElementById("todoInput"); |
| 49 | + let newTask = input.value.trim(); |
| 50 | + |
| 51 | + if (/[^A-Za-z\s]/.test(newTask)) { |
| 52 | + alert("Please enter letters and spaces only."); |
| 53 | + input.value = ""; |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (newTask !== "") { |
| 58 | + todos.push({ task: newTask, completed: false }); |
| 59 | + populateTodoList(todos); |
| 60 | + input.value = ""; |
| 61 | + } |
20 | 62 | } |
| 63 | +populateTodoList(todos); |
21 | 64 |
|
22 | | -// 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). |
23 | 65 | function deleteAllCompletedTodos() { |
24 | | - // Write your code here... |
| 66 | + todos = todos.filter((todo) => !todo.completed); |
| 67 | + populateTodoList(todos); |
25 | 68 | } |
| 69 | + |
| 70 | +document.getElementById("todo-form").addEventListener("submit", addNewTodo); |
| 71 | +document |
| 72 | + .getElementById("remove-all-completed") |
| 73 | + .addEventListener("click", deleteAllCompletedTodos); |
0 commit comments