forked from aina-cas/to-do-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
99 lines (81 loc) · 3.56 KB
/
app.js
File metadata and controls
99 lines (81 loc) · 3.56 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Toggle theme function
function changeTheme () {
document.body.classList.toggle('light');
}
const todoInput = document.querySelector("#todo-input");
// Array of objects to be used in later functions
const todos = [];
// Funtction to hadle the user input when they press enter
function handleTodoInput(event) {
// Check if the user pressed the enter key
if (event.key === "Enter" || event.keyCode === 13) {
// Add the new todo to the todos array
todos.push({value: event.target.value, checked: false});
//Call the newTodo function to create a new todo item in the html
newTodo(event.target.value);
//Clear the input field after new todo item is added
event.target.value = "";
}
}
// Add event listener to the input field to call the handleTodoInput function when enter is pressed
todoInput.addEventListener("keyup", handleTodoInput);
// Function to create new todo item element
function newTodo(value) {
// Create the html elements for the todo
const todo = document.createElement('div');
// Add draggable attribute to the div
todo.draggable = true;
const todoText = document.createElement('p');
const todoCheckBox = document.createElement('input');
const todoCheckBoxLabel = document.createElement('label');
const todoX = document.createElement('span');
//Set the text for the element
todoText.textContent = value;
//Set type and name for checkbox element
todoCheckBox.type = 'checkbox';
todoCheckBox.name = 'checkbox';
//Set the 'for' attribute on the label to match with the checkbox
todoCheckBoxLabel.htmlFor = 'checkbox';
//Add an event listener to the label that will toggle the checkbox and add/remove a class when clicked
todoCheckBoxLabel.addEventListener('click', function(e) {
if (todoCheckBox.checked) {
todoCheckBox.checked = false;
todoText.style.textDecoration = 'none';
todoCheckBoxLabel.classList.remove('active');
} else {
todoCheckBox.checked = true;
todoText.style.textDecoration = 'line-through';
todoCheckBoxLabel.classList.add('active');
}
});
todoX.textContent = "X";
// Add an event listener to the X elemnt so it will remove the todo item when clicked
todoX.addEventListener('click', function(e) {
e.target.parentElement.remove();
});
// Add classes to the todo element
todo.classList.add('todo');
todoCheckBoxLabel.classList.add('circle');
todoX.classList.add('x');
// Append the the elements to the todo item
todo.appendChild(todoCheckBox);
todo.appendChild(todoCheckBoxLabel);
todo.appendChild(todoText);
todo.appendChild(todoX);
// Get the container for todos
const todosContainer = document.querySelector('.todos-container');
// Append the todo to the container
todosContainer.appendChild(todo);
}
// further functions needed:
// Filter if a task already exists and if so, prevent from adding to the todos
// To check if the input is not empty and so the user can not add an empty todo item
// To store the list after the page is refrehed
// To show how many items are left to do on the list
// Filter to show all todo items
// Filter to show active items
// Filter to show already completed items
// Clear all the completed
// Drag and drop to reorder the list
//Just thought about how would a user using a phone would press enter?
//There might be more, but that's all I can think of for now.