Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
128 changes: 128 additions & 0 deletions 1. Building a To-Do List Without Framework/submission/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6"
crossorigin="anonymous"
/>
<link
href="https://unpkg.com/[email protected]/dist/aos.css"
rel="stylesheet"
/>
</head>
<body>
<div class="container-fluid">
<div class="py-5"></div>
<div class="row">
<div class="col-12 d-flex justify-content-center">
<button
type="button"
class="btn btn-primary"
data-bs-toggle="modal"
data-bs-target="#exampleModal"
>
ADD A TODO LIST
</button>
<div
class="modal fade"
id="exampleModal"
tabindex="-1"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
<div
class="modal-dialog modal-dialog-centered modal-dialog-scrollable"
>
<div class="modal-content">
<div class="modal-header">
<h5
class="modal-title"
id="exampleModalLabel"
>
ADD A TODO LIST
</h5>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div>
<form id="todo-form">
<div class="modal-body">
<div class="form-floating mb-3">
<input
type="text"
class="form-control"
id="floatingInput"
placeholder="Maths Homework"
name="todo-input"
/>
<label for="floatingInput"
>Title</label
>
</div>
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-outline-danger fw-bold"
data-bs-dismiss="modal"
>
Close
</button>
<button
type="submit"
class="btn btn-outline-success fw-bold"
>
Save changes
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<div id="confirmation" class="pt-5"></div>
<div id="todo-list"></div>

<div class="d-flex justify-content-center py-5">
<button
class="btn btn-outline-success"
id="save-todo"
onclick="{saveTodo()}"
>
Save
</button>
</div>
<div class="d-flex justify-content-center">
<button
class="btn btn-outline-success"
id="delete-todo"
onclick="{deleteTodo()}"
>
Clear All
</button>
</div>
<div class="py-5"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf"
crossorigin="anonymous"
></script>
<script src="https://unpkg.com/[email protected]/dist/aos.js"></script>
<script src="./index.js" type="text/javascript"></script>
<script>
AOS.init();
</script>
</body>
</html>
108 changes: 108 additions & 0 deletions 1. Building a To-Do List Without Framework/submission/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
let todoList = [];

function generateTodoItem(itemName) {
const timestamp = Date.now();
return {
id: "todo-item-" + timestamp,
name: itemName,
timestamp,
isDone: false,
};
}

function todoElement(item, i) {
const elem = document.createElement("div");
elem.id = item.id;
elem.innerHTML = `
<div class="row py-2 pb-2 text-center" data-aos="fade-up">
<div class="col-3 text-center">${i + 1}</div>
<div class="col-3"> <input type="checkbox" onchange="markItem(event)" ${
item.isDone ? "checked" : ""
}/></div>
<div class="col-3">${item.name}</div>
<div class="col-3" id=${
item.id
}><button class="btn btn-outline-danger" onclick="deleteItem(event)">Delete Item</button></div>
</div>
`;
return elem;
}

function renderList() {
const listElement = document.getElementById("todo-list");

listElement.innerHTML = "";

for (let i = 0; i < todoList.length; i++) {
const todoItem = todoElement(todoList[i], i);
listElement.appendChild(todoItem);
}

let alert = `
<div class="text-center alert ${
isAdded
? "alert-success"
: "alert-danger"
} alert-dismissible fade show shadow border-0" role="alert" data-aos="fade-down">
<strong>${
isAdded
? "Congraluations, Your item has been added."
: "Your item has been deleted."
}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
$("#confirmation").html(alert);
}

document
.getElementById("todo-form")
.addEventListener("submit", function (event) {
event.preventDefault();
const itemName = event.target["todo-input"].value;
if (itemName) {
todoList.push(generateTodoItem(itemName));
isAdded = true;
renderList();
event.target.reset();
}
});

function markItem(event) {
const itemId = event.target.parentElement.id; // getting the id of the parent element to search in the array
todoList.some(function (todoItem) {
if (todoItem.id === itemId) {
todoItem.isDone = !todoItem.isDone;
renderList();
return true;
}
});
}

function deleteItem(event) {
isAdded = false;
const itemId = event.target.parentElement.id; // getting the id of the parent element to search in the array
todoList.some(function (todoItem, i) {
if (todoItem.id === itemId) {
todoList.splice(i, 1);
renderList();
return true;
}
});
}

function saveTodo(e) {
localStorage.setItem("todoList", JSON.stringify(todoList));
}
function deleteTodo(e) {
localStorage.clear();
location.reload();
}

try {
if (localStorage.getItem("todoList")) {
todoList = JSON.parse(localStorage.getItem("todoList"));
renderList();
}
} catch (e) {
console.log(e);
}