-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
53 lines (46 loc) · 1.17 KB
/
todo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function Todo(id, title, isCompleted = false) {
this.id = id;
this.title = title;
this.isCompleted = isCompleted;
}
let todos = [];
let id = 0;
function addTodo() {
const title = document.getElementById("title").value;
const todo = new Todo(id, title);
id++;
todos.push(todo);
}
function deleteItem(el) {
const itemId = parseInt(el.parentNode.getAttribute("data"));
console.log(itemId);
todos = todos.filter(function (item) {
return item.id !== itemId;
});
draw();
}
const listContainer = document.getElementsByClassName("listContainer")[0];
document.addEventListener("keypress", function (event) {
if (event.keyCode === 13) {
addTodo();
console.log(todos);
draw();
}
});
document.getElementById("add").addEventListener("click", function () {
addTodo();
console.log(todos);
draw();
});
function draw() {
listContainer.innerHTML = "";
todos.forEach((item) => {
const list = `
<div class="listBox" data="${item.id}">
<div class="listText">${item.title}</div>
<i id="delete" onclick="deleteItem(this)" class="fa-solid fa-circle-xmark"></i>
</div>
`;
listContainer.innerHTML += list;
});
}