forked from tareq89/simple-todo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvanilla.js
96 lines (78 loc) · 3.05 KB
/
vanilla.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
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
// Get references to DOM elements
const todoInput = document.getElementById("todoInput");
const addTodoBtn = document.getElementById("addTodoBtn");
const todoList = document.getElementById("todoList");
// Add event listener to the "Add Todo" button
addTodoBtn.addEventListener("click", addTodo);
// Function to add a new todo item
function addTodo() {
// Get the trimmed value from the todo input field
const todoText = todoInput.value.trim();
// Check if the input is not empty
if (todoText !== "") {
// Create a new list item
const li = document.createElement("li");
// Create a div to hold the edit and delete buttons
const buttonsDiv = document.createElement("div");
buttonsDiv.classList.add("buttons");
// Create a span element to display the todo text
const span = document.createElement("span");
span.textContent = todoText;
// Create an Edit button
const editButton = document.createElement("button");
editButton.classList.add("link-button");
editButton.textContent = "Edit";
editButton.addEventListener("click", editTodo);
buttonsDiv.appendChild(editButton);
// Create a Delete button
const deleteButton = document.createElement("button");
deleteButton.classList.add("link-button");
deleteButton.textContent = "Delete";
deleteButton.addEventListener("click", deleteTodo);
buttonsDiv.appendChild(deleteButton);
// Clear the input field
todoInput.value = "";
// Append the elements to the list item
li.appendChild(span);
li.appendChild(buttonsDiv);
todoList.appendChild(li);
}
}
// Function to edit a todo item
function editTodo() {
// Prompt the user for the updated todo text
const newText = prompt("Enter updated todo:");
// Check if the prompt was not cancelled
if (newText !== null) {
// Get a reference to the parent list item
const li = this.parentElement.parentElement;
// Create a new span element with the updated text
const span = document.createElement("span");
span.textContent = newText;
// Clear the contents of the parent list item
li.innerHTML = "";
// Create a div to hold the edit and delete buttons
const buttonsDiv = document.createElement("div");
buttonsDiv.classList.add("buttons");
// Create an Edit button
const editButton = document.createElement("button");
editButton.classList.add("link-button");
editButton.textContent = "Edit";
editButton.addEventListener("click", editTodo);
buttonsDiv.appendChild(editButton);
// Create a Delete button
const deleteButton = document.createElement("button");
deleteButton.classList.add("link-button");
deleteButton.textContent = "Delete";
deleteButton.addEventListener("click", deleteTodo);
buttonsDiv.appendChild(deleteButton);
// Append the updated span and buttonsDiv to the parent list item
li.appendChild(span);
li.appendChild(buttonsDiv);
}
}
// Function to delete a todo item
function deleteTodo() {
// Remove the parent list item of the delete button
this.parentElement.parentElement.remove();
}