-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
115 lines (58 loc) · 2.24 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
let addbtn = document.getElementById('add-btn');
let taskInput = document.getElementById('task-input');
let taskSpace = document.getElementById('task-space');
addbtn.addEventListener('click', function(){
// using trim for remove extra spaces when the value is empty
let value = taskInput.value.trim();
taskInput.value = '';
if (value != '') {
addTask(value);
}
});
function addTask(value) {
// Create a new list item
let listItem = document.createElement('h4');
// Create a new div for btn and check
let actionContainer = document.createElement('div');
// Create a new delete button
let deleteButton = document.createElement('button');
// Create a new checkbox
let checkbox = document.createElement('input');
checkbox.type = 'checkbox';
// Adding the content
deleteButton.innerHTML = '<i class="fa-solid fa-xmark"></i>';
listItem.textContent = value;
let successMsg = document.createElement('p');
successMsg.textContent = 'The task has been completed';
successMsg.style.display = 'none';
// Create classes for styling
deleteButton.className = 'delete-btn btn';
listItem.className = 'task-list';
checkbox.className = 'checkbox';
actionContainer.className = 'action-container';
successMsg.className = 'success-msg';
// Append the checkbox, list item, and delete button in the desired order
listItem.appendChild(actionContainer);
actionContainer.appendChild(checkbox);
actionContainer.appendChild(deleteButton);
listItem.appendChild(successMsg);
// Append the list item to taskSpace
taskSpace.appendChild(listItem);
//function for delete button
deleteButton.addEventListener('click', function(){
listItem.remove();
})
//function for checkbox
checkbox.addEventListener('change', function(){
if (checkbox.checked)
{
// Show the success message when the checkbox is checked
successMsg.style.display = 'block';
}
else
{
// Hide the success message when the checkbox is unchecked
successMsg.style.display = 'none';
}
})
}