-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
48 lines (41 loc) · 1.23 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
let todoList = JSON.parse(localStorage.getItem('todoList')) || [
{
item: 'Buy Milk',
dueDate: '6/1/2024'
},
{
item: 'Go to college',
dueDate: '6/1/2024'
}
];
displayItems();
function addTODO() {
let inputElement = document.querySelector('#input-text');
let dateElement = document.querySelector('#todo-date');
let todoItem = inputElement.value;
let todoDate = dateElement.value;
todoList.push({ item: todoItem, dueDate: todoDate });
inputElement.value = '';
dateElement.value = '';
saveToLocalStorage();
displayItems();
}
function displayItems() {
let containerElement = document.querySelector('.todo-Container');
let newHtml = '';
for (let i = 0; i < todoList.length; i++) {
let { item, dueDate } = todoList[i];
newHtml += `
<span>${item}</span>
<span>${dueDate}</span>
<button class="btn-delete" onclick="deleteTodo(${i}); saveToLocalStorage(); displayItems();">Delete</button>
`;
}
containerElement.innerHTML = newHtml;
}
function deleteTodo(index) {
todoList.splice(index, 1);
}
function saveToLocalStorage() {
localStorage.setItem('todoList', JSON.stringify(todoList));
}