-
-
Notifications
You must be signed in to change notification settings - Fork 157
London | 25-ITP-May | Halimatou Saddiyaa | Sprint 3 | Todo list #765
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
base: main
Are you sure you want to change the base?
Changes from all commits
63e615a
70fd623
4bb42f0
9588977
32858f9
f96d991
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 |
---|---|---|
|
@@ -2,26 +2,44 @@ | |
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Title here</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<title>To Do List App</title> | ||
<link | ||
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" | ||
rel="stylesheet" | ||
/> | ||
<link rel="stylesheet" href="style.css" /> | ||
<link | ||
rel="stylesheet" | ||
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.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> | ||
<div class="todo-container"> | ||
<form onsubmit="addNewTodo(event)" id="todo-form"> | ||
<div> | ||
<input | ||
id="todoInput" | ||
type="text" | ||
placeholder="New todo..." | ||
class="form-control" | ||
/> | ||
</div> | ||
<div> | ||
<button type="button" id="add-btn" class="btn btn-primary mt-2"> | ||
Add Todo | ||
</button> | ||
<button | ||
type="button" | ||
id="remove-all-completed" | ||
class="btn btn-danger mt-2" | ||
> | ||
Remove all completed | ||
</button> | ||
</div> | ||
</form> | ||
<ul id="todo-list" class="list-group mt-3"></ul> | ||
</div> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,84 @@ | ||
function toggleTodoCompletion(todo) { | ||
todo.completed = !todo.completed; | ||
} | ||
|
||
function removeTodo(todos, todo) { | ||
return todos.filter((t) => t !== todo); | ||
} | ||
|
||
function deleteAllCompletedTodos() { | ||
todos = todos.filter((todo) => !todo.completed); | ||
populateTodoList(todos); | ||
} | ||
|
||
function createTodoElement(todo, todos) { | ||
let li = document.createElement("li"); | ||
li.textContent = todo.task; | ||
li.className = `list-group-item ${todo.completed ? "completed" : ""}`; | ||
|
||
let span = document.createElement("span"); | ||
span.className = "badge bg-light rounded-pill"; | ||
|
||
let checkIcon = document.createElement("i"); | ||
checkIcon.className = "fa fa-check todo-icon"; | ||
checkIcon.setAttribute("aria-hidden", "true"); | ||
checkIcon.addEventListener("click", () => { | ||
toggleTodoCompletion(todo); | ||
li.classList.toggle("completed"); | ||
}); | ||
|
||
let trashIcon = document.createElement("i"); | ||
trashIcon.className = "fa fa-trash todo-icon"; | ||
trashIcon.setAttribute("aria-hidden", "true"); | ||
trashIcon.addEventListener("click", () => { | ||
todos = removeTodo(todos, todo); | ||
populateTodoList(todos); | ||
}); | ||
|
||
span.appendChild(checkIcon); | ||
span.appendChild(trashIcon); | ||
li.appendChild(span); | ||
|
||
return li; | ||
} | ||
|
||
function populateTodoList(todos) { | ||
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. this function handles both data updates and UI changes, it works fine for a small task like this but when you work on much larger projects, you want to be able to reuse certain functions. in that case, keeping your code as modular as possible is the recommended approach. 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. I've refactored my code to create other functions to handle data updates and UI changes |
||
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. | ||
list.innerHTML = ""; | ||
todos.forEach((todo) => { | ||
list.appendChild(createTodoElement(todo, todos)); | ||
}); | ||
} | ||
|
||
// These are the same todos that currently display in the HTML | ||
// You will want to remove the ones in the current HTML after you have created them using JavaScript | ||
let todos = [ | ||
{ task: "Wash the dishes", completed: false }, | ||
{ task: "Do the shopping", completed: false }, | ||
]; | ||
|
||
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 input = document.getElementById("todoInput"); | ||
let newTask = input.value.trim(); | ||
|
||
// 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... | ||
if (newTask === "") { | ||
alert("Input field cannot be empty."); | ||
return; | ||
} | ||
|
||
if (/[^A-Za-z\s]/.test(newTask)) { | ||
alert("Please enter letters and spaces only."); | ||
input.value = ""; | ||
return; | ||
} | ||
|
||
todos.push({ task: newTask, completed: false }); | ||
populateTodoList(todos); | ||
input.value = ""; | ||
} | ||
|
||
document.getElementById("add-btn").addEventListener("click", addNewTodo); | ||
document | ||
.getElementById("remove-all-completed") | ||
.addEventListener("click", deleteAllCompletedTodos); | ||
|
||
populateTodoList(todos); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,65 @@ | ||
.completed { | ||
text-decoration: line-through; | ||
color: gray; | ||
} | ||
|
||
.todo-icon { | ||
cursor: pointer; | ||
margin-left: 8px; | ||
font-size: 1.2rem; | ||
padding: 8px; | ||
color: inherit; | ||
} | ||
|
||
.completed .fa-check { | ||
color: green; | ||
} | ||
|
||
.todo-icon:hover { | ||
opacity: 0.7; | ||
} | ||
|
||
.todo-container { | ||
max-width: 400px; | ||
margin: 50px auto; | ||
text-align: center; | ||
} | ||
|
||
.todo-container form > div { | ||
margin-bottom: 15px; | ||
} | ||
|
||
#todoInput { | ||
width: 100%; | ||
padding: 10px; | ||
font-size: 1rem; | ||
} | ||
|
||
button { | ||
border-radius: 5px; | ||
padding: 12px 20px; | ||
font-size: 1rem; | ||
cursor: pointer; | ||
min-height: 44px; | ||
} | ||
|
||
button:hover { | ||
opacity: 0.9; | ||
} | ||
|
||
.list-group-item { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
padding: 10px 15px; | ||
margin-bottom: 8px; | ||
} | ||
|
||
.badge { | ||
display: flex; | ||
gap: 8px; | ||
padding: 6px 10px; | ||
background-color: #f8f9fa; | ||
color: #000; | ||
border-radius: 50px; | ||
} |
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.
very good. it's much cleaner and reusable.