Skip to content

GLASGOW | May-2025 | Taras Mykytiuk | Module-Data-Groups | Sprint_3 | Todo_List #693

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions Sprint-3/todo-list/index.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<form>
<div>
<input type="text" placeholder="New todo..." />
</div>
<div>
<button type="submit">Add Todo</button>
<button
type="button"
id="remove-all-completed"
class="btn btn-primary mb-3"
>
Remove all completed
</button>
</div>
</form>
<script src="script.js"></script>
</body>
</html>

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<link rel="stylesheet" href="style.css" />
</head>

<body>
<form>
<div>
<input id="task-input" type="text" placeholder="New todo..." />
</div>
<div>
<button id="add-new-todo" type="submit">Add Todo</button>
<button type="button" id="remove-all-completed" class="btn btn-primary mb-3">
Remove all completed
</button>
</div>
</form>
<ul id="todo-list">
</ul>
</div>
<script src="script.js"></script>
</body>

</html>
58 changes: 55 additions & 3 deletions Sprint-3/todo-list/script.js
Original file line number Diff line number Diff line change
@@ -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.
}

Expand All @@ -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()
}
}
}