-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (35 loc) · 1.16 KB
/
index.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
const addBtn = document.getElementById('add-btn');
const input = document.getElementById('input-field');
const todosContainer = document.getElementById('todos');
let todos = [];
const renderTodos = () => {
todosContainer.innerHTML = '';
todos.forEach((todo, index) => {
const todoDiv = document.createElement('div');
todoDiv.classList.add('todo');
const todoSpan = document.createElement('span');
todoSpan.innerText = todo;
const removeButton = document.createElement('button');
removeButton.innerText = "X";
removeButton.classList.add('remove-btn');
removeButton.setAttribute("data-id", index);
todoDiv.append(todoSpan, removeButton);
todosContainer.appendChild(todoDiv);
});
}
addBtn.onclick = function () {
const todoText = input.value;
if (todoText) {
todos.push(todoText);
input.value = '';
renderTodos();
}
};
todosContainer.onclick = (event) => {
if (event.target.classList.contains('remove-btn')) {
const index = event.target.getAttribute('data-id');
todos.splice(index, 1);
renderTodos();
}
}
renderTodos();