-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
35 lines (30 loc) · 1.06 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
let todoList = JSON.parse(localStorage.getItem('todoList')) || []
displayItems();
function addTodo() {
let inputElement = document.querySelector('#todo-input');
let dateElement = document.querySelector('#todo-date');
let todoItem = inputElement.value;
let todoDate = dateElement.value;
todoList.push({item: todoItem, dueDate: todoDate});
saveToLocalStorage();
inputElement.value = '';
dateElement.value = '';
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 style="margin:20px;">${item}</span>
<span>${dueDate}</span>
<button class='btn-delete' onclick="todoList.splice(${i}, 1);
displayItems();saveToLocalStorage();">Delete</button>
`;
}
containerElement.innerHTML = newHtml;
}
function saveToLocalStorage() {
localStorage.setItem('todoList', JSON.stringify(todoList));
}