-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
108 lines (71 loc) · 3.07 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
//adding the task adder button going fromtop to bottom
//first thing is to do something on clicking add task button4
const taskAdder = document.querySelector('.add-task');//as our task adder is this add task class button
//value that the user enters is stored here
var userInput = document.querySelector('.user-input');
const taskContainer = document.querySelector('.task-container');
//we have created 2 constants now we have to add something to the taskContainer
//creating a div with class that holds the tasks
class tasks{
constructor(taskName){
this.createDiv(taskName);
}
createDiv(taskName){
let taskCheck = document.createElement('input');
taskCheck.checked = false;
taskCheck.classList.add('task-check');
taskCheck.type='checkbox';
taskCheck.addEventListener('click',countDoneTasks);
let taskInput = document.createElement('input');
taskInput.value = taskName;
taskInput.disabled = true;
taskInput.classList.add('task-input');
taskInput.type = "text";
let taskBox = document.createElement('div');
taskBox.classList.add('tasks');
let editTaskButton = document.createElement('button');
editTaskButton.classList.add('edit-task');
editTaskButton.innerHTML = '<i class="fas fa-edit"></i>';
let delTaskButton = document.createElement('button');
delTaskButton.classList.add('delete-task');
delTaskButton.innerHTML = '<i class="far fa-trash-alt"></i>';
taskContainer.appendChild(taskBox);
taskBox.appendChild(taskCheck);
taskBox.appendChild(taskInput);
taskBox.appendChild(editTaskButton);
taskBox.appendChild(delTaskButton);
//adding events to the button clicks
editTaskButton.addEventListener('click',()=> this.editFun(taskInput));
delTaskButton.addEventListener('click',()=>this.deleteFun(taskBox));
delTaskButton.addEventListener('click',countDoneTasks);
}
editFun(taskInput){
taskInput.disabled = !taskInput.disabled;
}
deleteFun(tasks){
taskContainer.removeChild(tasks);
}
}
//event listener for topmost add button
function addTask(){
if(userInput.value!=''){
new tasks(userInput.value);
userInput.value='';
}
}
taskAdder.addEventListener('click',addTask);
taskAdder.addEventListener('click',countDoneTasks);
//adding event listener to enter key being pressed when entered something
window.addEventListener('keydown',function(key){
if(key.keyCode==13){
addTask();
countDoneTasks();
}
});
function countDoneTasks(){
var noOfCheckBoxes = document.querySelectorAll('input[type="checkbox"]').length;
var noOfChecked = document.querySelectorAll('input[type="checkbox"]:checked').length;
var LeftTasks = noOfCheckBoxes - noOfChecked;
// alert(LeftTasks);
document.getElementById('counter').innerHTML="You have " + LeftTasks +" pending tasks";
}