-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
59 lines (47 loc) · 1.94 KB
/
script.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
49
50
51
52
53
54
55
56
57
58
59
document.getElementById("addButton").onclick = function () {
addItemToList();
};
document.getElementById("textBox").addEventListener("keydown", function (event) {
// Check if the pressed key is Enter (key code 13)
if (event.keyCode === 13) {
event.preventDefault(); // Prevent the default form submission behavior
addItemToList();
}
});
function addItemToList() {
let textBoxValue = document.getElementById("textBox").value;
if (textBoxValue.trim() !== "") {
// Create a new div to hold the todo item and checkbox
let todoItemDiv = document.createElement("div");
// Create a new checkbox
let checkbox = document.createElement("input");
checkbox.type = "checkbox";
// Create a new paragraph element for the todo item
let newTodo = document.createElement("p");
newTodo.textContent = textBoxValue;
// Append the checkbox and todo item to the todoItemDiv
todoItemDiv.appendChild(checkbox);
todoItemDiv.appendChild(newTodo);
// Append the todoItemDiv to the todoList div
document.getElementById("todoList").appendChild(todoItemDiv);
// Clear the textbox
document.getElementById("textBox").value = "";
// Check if the todo list is empty and hide/show the todoList element accordingly
let todoList = document.getElementById("todoList");
if (todoList.children.length === 0) {
todoList.style.display = "none";
} else {
todoList.style.display = "flex";
}
}
}
// Check if the todo list is empty and hide/show the todoList element accordingly ///////// This one exists to remove the todoList elements when the page is loaded. for some reason it won't be removed with the same code in the function.
if (todoList.children.length === 0) {
todoList.style.display = "none";
} else {
todoList.style.display = "flex";
}
function validateForm() {
// You can add validation logic here if needed
return true;
}