-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (77 loc) · 2.88 KB
/
Copy pathscript.js
File metadata and controls
79 lines (77 loc) · 2.88 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
const todoForm = document.querySelector('form');
const todoInput = document.getElementById('todo-box');
const todoListUl = document.getElementById('todo-list');
let allTodos = getTodos();
updateTodoList();
todoForm.addEventListener('submit', function (e) {
e.preventDefault();
addTodo();
})
function addTodo() {
const todoText = todoInput.value.trim();
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) => {
todoItem = createTodoItem(todo, todoIndex);
todoListUl.append(todoItem);
})
}
function createTodoItem(todo, todoIndex) {
const todoId = "todo-check-" + todoIndex;
const todoLi = document.createElement("li");
const todoText = todo.text;
todoLi.className = "todo";
todoLi.innerHTML = `
<input type="checkbox" id="${todoId}">
<label for="${todoId}" class="custom-checkbox">
<svg fill="transparent" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="green">
<path
d="M9 16.17l-3.88-3.88a1 1 0 00-1.41 1.41l4.59 4.59a1 1 0 001.41 0l10-10a1 1 0 00-1.41-1.41L9 16.17z" />
</svg>
</label>
<label for="${todoId}" class="todo-text" id="todo-text">
${todoText}
</label>
<button class="delete-button">
<svg fill="var(--secondary-color)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="red">
<path
d="M6 7v13a2 2 0 002 2h8a2 2 0 002-2V7H6zm3 11a1 1 0 01-2 0V9a1 1 0 012 0v9zm4 0a1 1 0 01-2 0V9a1 1 0 012 0v9zm4 0a1 1 0 01-2 0V9a1 1 0 012 0v9zM15.5 4l-.79-.79A1 1 0 0014 3H10a1 1 0 00-.71.29L8.5 4H5a1 1 0 000 2h14a1 1 0 000-2h-3.5z" />
</svg>
</button>
`
const deleteButton = todoLi.querySelector(".delete-button");
deleteButton.addEventListener("click", () => {
deleteTodoItem(todoIndex);
})
const checkbox = todoLi.querySelector("input");
checkbox.addEventListener("change", ()=>{
allTodos[todoIndex].completed = checkbox.checked;
saveTodos();
})
checkbox.checked = todo.completed;
return todoLi;
}
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);
}