Skip to content

Commit 327054b

Browse files
committed
added dynamic nature to the site with js
1 parent 3d73b19 commit 327054b

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

TodoList/index.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const input = document.querySelector('.tab');
2+
const addButton = document.querySelector('.addToDo');
3+
const todoContainer = document.querySelector('.todo');
4+
const countDisplay = document.querySelector('.count');
5+
const removeButton = document.querySelector('.removeChecked');
6+
let completedCount = 0;
7+
let totalCount = 0;
8+
9+
addButton.addEventListener('click', function (e) {
10+
e.preventDefault();
11+
const paragraph1 = document.createElement('div');
12+
const paragraph2 = document.createElement('div');
13+
const cont = document.createElement('div');
14+
const checkbox = document.createElement('input');
15+
checkbox.type = 'checkbox';
16+
paragraph1.appendChild(checkbox);
17+
const text = document.createTextNode(input.value);
18+
paragraph1.appendChild(text);
19+
const editButton = document.createElement('button');
20+
editButton.textContent = '✒️';
21+
paragraph2.appendChild(editButton);
22+
const deleteButton = document.createElement('button');
23+
deleteButton.textContent = '❎';
24+
paragraph2.appendChild(deleteButton);
25+
cont.appendChild(paragraph1);
26+
cont.appendChild(paragraph2);
27+
todoContainer.appendChild(cont);
28+
input.value = '';
29+
totalCount++;
30+
updateCountDisplay();
31+
checkbox.addEventListener('click', function () {
32+
if (checkbox.checked) {
33+
cont.style.textDecoration = 'line-through';
34+
completedCount++;
35+
} else {
36+
cont.style.textDecoration = 'none';
37+
completedCount--;
38+
}
39+
updateCountDisplay();
40+
});
41+
editButton.addEventListener('click', function () {
42+
const editText = prompt('Edit task:', text.textContent);
43+
if (editText !== null) {
44+
text.textContent = editText;
45+
}
46+
});
47+
deleteButton.addEventListener('click', function () {
48+
todoContainer.removeChild(cont);
49+
totalCount--;
50+
if (checkbox.checked) {
51+
completedCount--;
52+
}
53+
updateCountDisplay();
54+
});
55+
});
56+
57+
function updateCountDisplay() {
58+
countDisplay.textContent = `${completedCount} of ${totalCount} tasks done`;
59+
const completionRatio = completedCount / totalCount;
60+
if (completionRatio < 0.25) {
61+
countDisplay.style.backgroundimage = '#ff6347';
62+
} else if (completionRatio < 0.5) {
63+
countDisplay.style.backgroundColor = '#ffd700';
64+
} else if (completionRatio < 0.75) {
65+
countDisplay.style.backgroundColor = '#7cfc00';
66+
} else {
67+
countDisplay.style.backgroundColor = '#00ff7f';
68+
}
69+
//countDisplay.style.backgroundimage='linear-gradient(to right,yellow completionRatio%,transparent)';
70+
}
71+
72+
removeButton.addEventListener('click', function () {
73+
const checkboxes = todoContainer.querySelectorAll('input[type=checkbox]');
74+
checkboxes.forEach(function (checkbox) {
75+
if (checkbox.checked) {
76+
const cont = checkbox.parentNode.parentNode;
77+
todoContainer.removeChild(cont);
78+
totalCount--;
79+
if (checkbox.checked) {
80+
completedCount--;
81+
}
82+
}
83+
});
84+
updateCountDisplay();
85+
});

0 commit comments

Comments
 (0)