|
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. |
4 |
| -} |
| 3 | + todos.forEach((element) => { |
| 4 | + const listing = document.createElement("li"); |
| 5 | + const rem = document.createElement("span"); |
| 6 | + const add = document.createElement("span"); |
| 7 | + const dateCale = document.createElement("span"); |
| 8 | + const dateInput = document.createElement("input"); |
| 9 | + dateInput.type = "date"; |
| 10 | + dateInput.classList.add("inputing"); |
| 11 | + dateInput.style.display = "none"; |
| 12 | + rem.textContent = "🗑"; |
| 13 | + add.textContent = "✅"; |
| 14 | + dateCale.textContent = "📅"; |
| 15 | + listing.appendChild(rem); |
| 16 | + listing.appendChild(add); |
| 17 | + listing.appendChild(dateCale); |
| 18 | + listing.appendChild(dateInput); |
| 19 | + const taskText = document.createTextNode(element.task); |
| 20 | + listing.appendChild(taskText); |
| 21 | + list.appendChild(listing); |
| 22 | + let clickCount = 0; |
| 23 | + dateCale.addEventListener("click", () => { |
| 24 | + if (dateInput.style.display === "none") { |
| 25 | + dateInput.style.display = "block"; |
| 26 | + } else { |
| 27 | + dateInput.style.display = "none"; |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + |
| 32 | + add.addEventListener("click", () => { |
| 33 | + clickCount++; |
| 34 | + let isEvenClick = clickCount % 2 === 0; |
| 35 | + |
| 36 | + |
| 37 | + if (isEvenClick) { |
| 38 | + listing.style.textDecoration = "none"; |
| 39 | + } else { |
| 40 | + listing.style.textDecoration = "line-through"; |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + |
| 45 | + rem.addEventListener("click", () => { |
| 46 | + listing.remove(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + } |
5 | 50 |
|
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 | 51 | let todos = [
|
9 | 52 | { task: "Wash the dishes", completed: false },
|
10 | 53 | { task: "Do the shopping", completed: false },
|
11 | 54 | ];
|
12 | 55 |
|
| 56 | + |
13 | 57 | populateTodoList(todos);
|
14 | 58 |
|
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 | 59 | function addNewTodo(event) {
|
17 |
| - // The code below prevents the page from refreshing when we click the 'Add Todo' button. |
18 |
| - event.preventDefault(); |
19 |
| - // Write your code here... and remember to reset the input field to be blank after creating a todo! |
20 |
| -} |
| 60 | + const inputTodo = document.getElementById("textboxinput"); |
| 61 | + const buttonTodo = document.querySelector("button"); |
| 62 | + buttonTodo.addEventListener("click", (event) => { |
| 63 | + event.preventDefault(); |
| 64 | + const newList = document.createElement("li"); |
| 65 | + let list = document.getElementById("todo-list"); |
| 66 | + const tex = document.createTextNode(inputTodo.value); |
| 67 | + inputTodo.value = ""; |
| 68 | + const rem = document.createElement("span"); |
| 69 | + const add = document.createElement("span"); |
| 70 | + const dateCale = document.createElement("span"); |
| 71 | + const dateInput = document.createElement("input"); |
| 72 | + dateInput.type = "date"; |
| 73 | + dateInput.classList.add("inputing"); |
| 74 | + dateInput.style.display = "none"; |
| 75 | + rem.textContent = "🗑"; |
| 76 | + add.textContent = "✅"; |
| 77 | + dateCale.textContent = "📅"; |
| 78 | + newList.appendChild(rem); |
| 79 | + newList.appendChild(add); |
| 80 | + newList.appendChild(dateCale); |
| 81 | + newList.appendChild(dateInput); |
| 82 | + newList.append(tex); |
| 83 | + list.appendChild(newList); |
| 84 | + |
| 85 | + |
| 86 | + let clickCount = 0; |
21 | 87 |
|
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). |
| 88 | + |
| 89 | + dateCale.addEventListener("click", () => { |
| 90 | + if (dateInput.style.display === "none") { |
| 91 | + dateInput.style.display = "block"; |
| 92 | + } else { |
| 93 | + dateInput.style.display = "none"; |
| 94 | + } |
| 95 | + }); |
| 96 | + add.addEventListener("click", () => { |
| 97 | + clickCount++; |
| 98 | + let isEvenClick = clickCount % 2 === 0; |
| 99 | + |
| 100 | + |
| 101 | + if (isEvenClick) { |
| 102 | + newList.style.textDecoration = "none"; |
| 103 | + } else { |
| 104 | + newList.style.textDecoration = "line-through"; |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + |
| 109 | + rem.addEventListener("click", () => { |
| 110 | + newList.remove(); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + |
| 115 | + } |
| 116 | +addNewTodo(); |
23 | 117 | function deleteAllCompletedTodos() {
|
24 |
| - // Write your code here... |
| 118 | + const del = document.getElementById("remove-all-completed"); |
| 119 | + let list = document.getElementById("todo-list"); |
| 120 | + del.addEventListener("click", () => { |
| 121 | + list.textContent = ""; |
| 122 | + }); |
| 123 | + |
25 | 124 | }
|
| 125 | +deleteAllCompletedTodos(); |
| 126 | + |
| 127 | + |
| 128 | + |
0 commit comments