-
-
Notifications
You must be signed in to change notification settings - Fork 156
CYF-ITP-South Africa | Serna Malala | Module-Data-Groups | Week 3 Todo List #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2175276
3d911f2
606a493
50b4f37
b4cb908
8c72cb4
ebb066d
1014ced
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,88 @@ | ||
| function populateTodoList(todos) { | ||
| let list = document.getElementById("todo-list"); | ||
| // Write your code to create todo list elements with completed and delete buttons here, all todos should display inside the "todo-list" element. | ||
| let body = document.querySelector("body"); | ||
|
|
||
| list.innerHTML = ""; | ||
|
|
||
| let todoButtonsIndex = 0; | ||
|
|
||
| let todoButtons = `<span class="badge bg-primary rounded-pill"> | ||
| <i class="fa-solid fa-check" aria-hidden="true"></i> | ||
| <i class="fa fa-trash" aria-hidden="true"></i> | ||
| </span>`; | ||
|
|
||
| todos.forEach((todoItem, index) => { | ||
|
|
||
| let listItem = document.createElement("li"); | ||
| listItem.id = index; | ||
|
|
||
| let breakLine = document.createElement("br"); | ||
| let span = document.createElement("span"); | ||
|
|
||
|
|
||
| span.innerHTML = todoButtons; | ||
| listItem.style.background = "lightblue"; | ||
| listItem.style.padding = "1em"; | ||
| listItem.style.display = "inline-block"; | ||
| listItem.style.margin = "0.5em"; | ||
| listItem.textContent = todoItem.task; | ||
| list.style.margin = "0 auto"; | ||
| list.style.backgroundColor = "#FCC4AB"; | ||
| body.style.textAlign = "center" | ||
|
Comment on lines
+24
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not set CSS styles in |
||
|
|
||
| list.appendChild(breakLine); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding which is considered an error in HTML. Can you think of a better way to create space between list items using only CSS? |
||
|
|
||
| if (todoItem.completed === true) { | ||
| listItem.style.textDecoration = "line-through"; | ||
| } | ||
| listItem.appendChild(span); | ||
| list.appendChild(listItem); | ||
| //target the span instead of targetin the document, to make it more precise | ||
| const checkMark = span.querySelector(".fa-check"); | ||
| const deleteButton = span.querySelector(".fa-trash"); | ||
|
|
||
| checkMark.id = todoButtonsIndex; | ||
| deleteButton.id = todoButtonsIndex; | ||
| todoButtonsIndex++; | ||
|
Comment on lines
+44
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| //when the checkmark or trash buttons are clicked | ||
| //make span be inside the listItem element and target the nearest listItem | ||
|
|
||
|
|
||
| checkMark.addEventListener("click", function () { | ||
|
|
||
| const listItem = this.closest("li"); | ||
| const todoID = listItem.id; | ||
|
|
||
| if (todoID && todos[parseInt(todoID)].completed === false) { | ||
| todos[parseInt(todoID)].completed = true; | ||
| listItem.style.textDecoration = "line-through"; | ||
|
|
||
| } | ||
| else { | ||
| todos[parseInt(todoID)].completed = false; | ||
| listItem.style.textDecoration = "none"; | ||
| } | ||
|
Comment on lines
+57
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just call Any chance |
||
|
|
||
| }) | ||
|
|
||
|
|
||
| deleteButton.addEventListener("click", function () { | ||
| const listItem = this.closest("li"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know what |
||
| const todoID = listItem.id; | ||
|
|
||
| if (todoID) { | ||
| todos.splice(parseInt(todoID), 1); | ||
| listItem.remove(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is line 76 necessary? The next statement, |
||
| populateTodoList(todos); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| }) | ||
|
|
||
|
|
||
| }); | ||
| } | ||
|
|
||
| // These are the same todos that currently display in the HTML | ||
|
|
@@ -12,14 +94,51 @@ let todos = [ | |
|
|
||
| populateTodoList(todos); | ||
|
|
||
|
|
||
| // 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. | ||
| 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! | ||
|
|
||
| let inputField = document.getElementById("todo-input"); | ||
| let inputFieldValue = inputField.value; | ||
| let completedStatus = false; | ||
|
|
||
| let todo = {}; | ||
|
|
||
| if (inputFieldValue.length > 0) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to accept any input containing only space characters? |
||
| todo["task"] = inputFieldValue | ||
| todo["completed"] = completedStatus; | ||
| todos.push(todo); | ||
|
|
||
| console.log(`Added new Todo - Task:${todo.task}: Status:${todo.completed}`); | ||
| populateTodoList(todos); | ||
| } | ||
| else { | ||
| alert("Please input a new todo item!!"); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| //need to target form when the button is clicked | ||
|
|
||
| let form = document.querySelector("form"); | ||
| form.addEventListener("submit", addNewTodo); | ||
|
|
||
| // 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). | ||
|
|
||
| let removeButton = document.getElementById("remove-all-completed"); | ||
| function deleteAllCompletedTodos() { | ||
| // Write your code here... | ||
|
|
||
| todos = todos.filter((todoItem) => { | ||
| return todoItem.completed === false; | ||
| }) | ||
|
|
||
| populateTodoList(todos); | ||
| } | ||
|
|
||
| removeButton.addEventListener("click", deleteAllCompletedTodos); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
todoButtonsdoes not need to be reassigned a different value later, it is better to declare it usingconst.