-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
61 lines (47 loc) · 1.45 KB
/
script.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
54
55
56
57
58
59
60
const todos = JSON.parse(localStorage.getItem('saved-todos')) || [];
const addTodoButton = document.querySelector('.add-todo');
const todoName = document.querySelector('.todo-name');
const todoInput = document.querySelector('.todo-input');
const todoDuedateElement = document.querySelector('.todo-due-date');
const todosList = document.querySelector('.todos');
renderTodos()
addTodoButton.addEventListener('click', addTodo);
function renderTodos() {
if (todos.length === 0) {
todosList.innerHTML = `<tr><td colspan="5" class="text-center">No task found</td></tr>`;
return;
}
let todosHTML = '';
todos.forEach(function(todoObject, i) {
// const name = todoObject.name;
// const dueDate = todoObject.dueDate;
const {name, dueDate} = todoObject;
const html = `
<div class="todo">${name}</div>
<div class="todo">${dueDate}</div>
<button class="delete-todo" onclick="
todos.splice(${i}, 1);
renderTodos();
localStorage.removeItem('saved-todos');
">Delete</button>
`;
todosHTML += html;
});
todosList.innerHTML = todosHTML;
}
function addTodo() {
const todo = todoName.value;
const todoDuedate = todoDuedateElement.value;
todos.push({
name: todo,
dueDate: todoDuedate
});
todoName.value = '';
renderTodos();
localStorage.setItem('saved-todos', JSON.stringify(todos));
}
todoInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
addTodo();
}
});