-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (40 loc) · 1.37 KB
/
app.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
let inputElement = document.querySelector('input');
let formElement = document.querySelector('form');
let listElement = document.querySelector('ul');
let totalTaskElements = document.querySelector('#total-tasks');
let tasklist = []
function deleteItem(e) {
let task = e.target.parentElement.previousElementSibling.innerHTML;
let index = tasklist.indexOf(task);
if(index != -1) tasklist.splice(index, 1);
populateList()
}
function populateList() {
listElement.innerHTML = '';
tasklist.forEach(function(item){
let newItem = document.createElement('li');
let span = document.createElement('span');
span.innerHTML = item;
newItem.appendChild(span);
let anchorElement = document.createElement('a');
anchorElement.classList.add('delete');
anchorElement.innerHTML = '<i class = "fas fa-trash-alt"></i>';
newItem.appendChild(anchorElement);
anchorElement.addEventListener('click', (e) => deleteItem(e));
listElement.appendChild(newItem);
});
totalTaskElements.innerHTML = tasklist.length;
inputElement.value = '';
}
populateList();
function addTask() {
let s = inputElement.value.trim();
if(s && !tasklist.includes(s)) {
tasklist.push(s);
populateList();
}
}
formElement.addEventListener('submit', function(e) {
e.preventDefault();
addTask();
});