-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
57 lines (44 loc) · 1.31 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
const todoList = JSON.parse(localStorage.getItem('todoList')) || [];
renderTodoList();
function renderTodoList() {
let todoListHTML = '';
for (let i = 0; i < todoList.length; i++) {
const todoObject = todoList[i];
const { name, dueDate } = todoObject;
const html = `
<div>${name}</div>
<div>${dueDate}</div>
<button onclick="
deleteTodo(${i});
" class="delete-todo-button">Delete</button>
`;
todoListHTML += html;
}
document.querySelector('.js-todo-list')
.innerHTML = todoListHTML;
}
function addTodo() {
const inputElement = document.querySelector('.js-name-input');
const name = inputElement.value.trim();
const dateInputElement = document.querySelector('.js-due-date-input');
const dueDate = dateInputElement.value;
if (name === '' || dueDate === '') {
alert('Please enter both task name and due date.');
return;
}
todoList.push({
name,
dueDate
});
inputElement.value = '';
saveToLocalStorage();
renderTodoList();
}
function deleteTodo(index) {
todoList.splice(index, 1);
saveToLocalStorage();
renderTodoList();
}
function saveToLocalStorage() {
localStorage.setItem('todoList', JSON.stringify(todoList));
}