Skip to content

Commit 0e288ea

Browse files
committed
added the simple todo app implementation
1 parent d3e7dce commit 0e288ea

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

todo-list/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>todo list</title>
8+
</head>
9+
<body>
10+
<div id="todo-container">
11+
<label for="todo-item">Enter the todo item:</label>
12+
<input type="text" id="todo-item" />
13+
<!-- <button type="submit" onclick="">Submit</button> -->
14+
</div>
15+
<div id="todo-item-list"></div>
16+
<script src="./index.js"></script>
17+
</body>
18+
</html>

todo-list/index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
let todos = [];
2+
const todoItemDiv = document.querySelector("#todo-item-list");
3+
const inputText = document.querySelector("#todo-item");
4+
5+
function addTodo(todo) {
6+
todos.push(todo);
7+
inputText.value = "";
8+
renderList();
9+
}
10+
11+
function deleteTodo(todoId) {
12+
const newTodos = todos.filter((todo) => {
13+
return todo.id !== todoId;
14+
});
15+
todos = newTodos;
16+
renderList();
17+
}
18+
19+
function renderList() {
20+
todoItemDiv.innerHTML = "";
21+
for (let i = 0; i < todos.length; i++) {
22+
const todo = todos[i];
23+
24+
const li = document.createElement("li");
25+
26+
li.innerHTML = `
27+
<span id="${todo.id}">${todo.text}</span>
28+
<button data-id=${todo.id} class="delete">Delete</button>`;
29+
30+
todoItemDiv.appendChild(li);
31+
}
32+
}
33+
34+
function handleClick(e) {
35+
console.log(e.target.className);
36+
if (e.target.className === "delete") {
37+
const todoId = Number(e.target.dataset.id);
38+
deleteTodo(todoId);
39+
}
40+
}
41+
42+
function initialize() {
43+
document.addEventListener("click", handleClick);
44+
document.addEventListener("keyup", function (e) {
45+
const text = e.target.value;
46+
if (e.key === "Enter") {
47+
todo = {
48+
text: text,
49+
id: Date.now(),
50+
done: false,
51+
};
52+
53+
addTodo(todo);
54+
}
55+
});
56+
57+
renderList();
58+
}
59+
60+
initialize();

0 commit comments

Comments
 (0)