-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (79 loc) · 3.04 KB
/
app.js
File metadata and controls
83 lines (79 loc) · 3.04 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const todoForm = document.querySelector('form');
const todoInput = document.getElementById('todo-input');
const todoListUL = document.getElementById('todo-list');
let allTodos = getTodos();
updateTodoList();
todoForm.addEventListener('submit', function(e){
e.preventDefault();
addTodo();
});
function addTodo(){
const todoText = todoInput.value;
if(todoText.length > 0){
const todoObject = {
text: todoText,
completed: false
}
allTodos.push(todoObject);
updateTodoList();
saveTodos();
todoInput.value = "";
};
};
function updateTodoList(){
todoListUL.innerHTML = "";
allTodos.forEach((todo, todoIndex)=>{
let todoItem = createTodoItem(todo, todoIndex);
todoListUL.append(todoItem);
});
};
function createTodoItem(todo, todoIndex){
const todoId = "todo-"+todoIndex;
const todoLI = document.createElement("li");
const todoText = todo.text;
todoLI.className = "todo";
todoLI.innerHTML = `<input type="checkbox" id="${todoId}">
<label class="custom-checkbox" for="${todoId}">
<svg fill="transparent"xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 -960 960 960" width="20px" fill="#000000">
<path d="M389-267 195-460l51-52 143 143 325-324 51 51-376 375Z" />
</svg>
</label>
<label for="${todoId}" class="todo-text">
${todoText}
</label>
<button class="delete-button">
<svg fill="var(--secondary-color)"xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 -960 960 960" width="20px" fill="#000000">
<path
d="M312-144q-29.7 0-50.85-21.15Q240-186.3 240-216v-480h-48v-72h192v-48h192v48h192v72h-48v479.57Q720-186 698.85-165T648-144H312Zm336-552H312v480h336v-480ZM384-288h72v-336h-72v336Zm120 0h72v-336h-72v336ZM312-696v480-480Z" />
</svg>
</button>
`;
const deleteButton = todoLI.querySelector('.delete-button');
deleteButton.addEventListener('click', ()=>{
deleteTodo(todoIndex);
});
const checkbox = todoLI.querySelector("input");
checkbox.addEventListener("change", ()=>{
allTodos[todoIndex].completed = checkbox.checked;
saveTodos();
});
checkbox.checked = todo.completed;
return todoLI;
};
function deleteTodo(todoIndex) {
allTodos.splice(todoIndex, 1);
updateTodoList();
}
function deleteTodoItem(todoIndex){
allTodos = allTodos.filter((_, i)=> i !== todoIndex);
saveTodos();
updateTodoList();
}
function saveTodos(){
const todosJson = JSON.stringify(allTodos);
localStorage.setItem("todos", todosJson);
};
function getTodos(){
const todos = localStorage.getItem("todos") || "[]";
return JSON.parse(todos);
};