-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (56 loc) · 1.89 KB
/
script.js
File metadata and controls
67 lines (56 loc) · 1.89 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
const classNames = {
TODO_ITEM: 'todo-container',
TODO_CHECKBOX: 'todo-checkbox',
TODO_TEXT: 'todo-text',
TODO_DELETE: 'todo-delete',
}
const list = document.getElementById('todo-list')
const itemCountSpan = document.getElementById('item-count')
const uncheckedCountSpan = document.getElementById('unchecked-count')
function newTodo() {
let userInput = prompt("Enter a task description");
if (userInput != null && userInput.length > 0) {
var unorderedListItem = document.createElement("LI");
unorderedListItem.className = classNames.TODO_ITEM;
unorderedListItem.id = getUniqueID();
var checkBox = document.createElement("INPUT");
checkBox.setAttribute("type", "checkbox");
checkBox.className = classNames.TODO_CHECKBOX;
checkBox.onchange = function () {updateUncheckedItemsCount()}
var todoText = document.createElement("SPAN");
todoText.className = classNames.TODO_TEXT;
todoText.textContent = userInput;
var deleteButton = document.createElement("BUTTON");
deleteButton.textContent = "Delete";
deleteButton.className = classNames.TODO_DELETE;
deleteButton.onclick = function () {
removeListItem(unorderedListItem.id);
};
unorderedListItem.appendChild(checkBox);
unorderedListItem.appendChild(todoText);
unorderedListItem.appendChild(deleteButton);
list.appendChild(unorderedListItem);
updateItemCount();
updateUncheckedItemsCount()
}
}
function getUniqueID(){
return Date.now()
}
function removeListItem(id){
list.removeChild(document.getElementById(id))
updateItemCount()
}
function updateItemCount(){
itemCountSpan.innerHTML = list.childElementCount
updateUncheckedItemsCount()
}
function updateUncheckedItemsCount(){
let uncheckedCount = 0;
list.childNodes.forEach(item => {
if (item.firstChild.checked == false){
uncheckedCount++
}
})
uncheckedCountSpan.innerHTML = uncheckedCount
}