-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
36 lines (31 loc) · 947 Bytes
/
app.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
const todoForm = document.querySelector('form');
const todoInput = document.querySelector('#todo-input');
const todoList = document.querySelector('#todo-list');
let todos = [];
function addTodo() {
event.preventDefault();
const todoText = todoInput.value;
if (todoText) {
const todo = { id: Date.now(), text: todoText };
todos.push(todo);
displayTodo();
todoInput.value = '';
}
}
function displayTodo() {
todoList.innerHTML = '';
todos.forEach(function(todo) {
const li = document.createElement('li');
li.innerHTML = '<span>' + todo.text + '</span><button data-id="' + todo.id + '">Sil</button>';
todoList.appendChild(li);
});
}
function deleteTodo() {
const id = parseInt(event.target.getAttribute('data-id'));
todos = todos.filter(function(todo) {
return todo.id !== id;
});
displayTodo();
}
todoForm.addEventListener('submit', addTodo);
todoList.addEventListener('click', deleteTodo);