Skip to content

Commit 5b6950e

Browse files
committed
Todo List with Array, Function, Object, DOM
1 parent d7b0349 commit 5b6950e

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

Projects/todo list/todo.css

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
body {
2+
font-family: Arial;
3+
}
4+
5+
#main-heading {
6+
text-align: center;
7+
}
8+
9+
.grid-container {
10+
display: grid;
11+
grid-template-columns: 200px 150px 100px;
12+
column-gap: 10px;
13+
row-gap: 10px;
14+
margin-top: 10px;
15+
align-items: center;
16+
}
17+
18+
#todo-input, #todo-date {
19+
font-size: 15px;
20+
padding: 10px 5px;
21+
}
22+
23+
.btn-delete {
24+
background-color: red;
25+
color: white;
26+
border: none;
27+
border-radius: 10px;
28+
padding: 15px 0px;
29+
cursor: pointer;
30+
}
31+
32+
.btn-add {
33+
background-color: green;
34+
color: white;
35+
border: none;
36+
border-radius: 10px;
37+
padding: 15px 0px;
38+
cursor: pointer;
39+
}

Projects/todo list/todo.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Todo App</title>
5+
<link rel="styleSheet" href="todo.css" />
6+
</head>
7+
8+
<body>
9+
<h1 id="main-heading">Todo App</h1>
10+
11+
<div class="grid-container">
12+
<input id="todo-input" type="text" placeholder="Enter Todo Here" />
13+
<input id="todo-date" type="date" />
14+
15+
<button class="btn-add" onclick="addTodo();">Add</button>
16+
</div>
17+
18+
<div class="todo-container grid-container"></div>
19+
20+
<!-- <p id="todo-items"></p> -->
21+
22+
<script src="todo.js"></script>
23+
</body>
24+
</html>

Projects/todo list/todo.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
let todoList = [
2+
{
3+
item: "Buy Milk",
4+
dueDate: "6/16/2025",
5+
},
6+
{
7+
item: "Go to College",
8+
dueDate: "6/16/2025",
9+
},
10+
];
11+
12+
displayItems();
13+
14+
function addTodo() {
15+
let inputElement = document.querySelector("#todo-input");
16+
17+
let dateElement = document.querySelector("#todo-date");
18+
19+
let todoItem = inputElement.value;
20+
let todoDate = dateElement.value;
21+
//console.log(todoElement);
22+
23+
todoList.push({ item: todoItem, dueDate: todoDate });
24+
inputElement.value = "";
25+
26+
displayItems();
27+
}
28+
29+
function displayItems() {
30+
let containerElement = document.querySelector(".todo-container");
31+
32+
let newHtml = "";
33+
for (let i = 0; i < todoList.length; i++) {
34+
//let item = todoList[i].item;
35+
//let dueDate = todoList[i].dueDate;
36+
37+
// destructing object
38+
let { item, dueDate } = todoList[i];
39+
newHtml += `
40+
<span>${item}</span>
41+
<span>${dueDate}</span>
42+
<button class ="btn-delete" onClick="todoList.splice(${i}, 1); displayItems()">Delete</button>
43+
`;
44+
}
45+
containerElement.innerHTML = newHtml;
46+
}
47+
48+
/* function displayItems() {
49+
let displayElement = document.querySelector("#todo-items");
50+
51+
displayElement.innerText = ""; // Clears the text inside the element (reset it).
52+
for (let i = 0; i < todoList.length; i++) {
53+
displayElement.innerText = todoList.join("\n");
54+
55+
displayElement.innerText = todoList
56+
.map((item, index) => `${index + 1}. ${item}`)
57+
.join("\n");
58+
59+
displayElement.innerText = displayElement.innerText + "\n" + todoList[i];
60+
}
61+
}
62+
*/

0 commit comments

Comments
 (0)