-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (52 loc) · 1.64 KB
/
Copy pathscript.js
File metadata and controls
67 lines (52 loc) · 1.64 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
let inputelement = document.querySelector('input');
let formelement = document.querySelector('form');
let listelement = document.querySelector('ul');
let totaltaskselement = document.querySelector('#total-tasks');
let tasklist = [
'Buy Groceries',
'Car Service'
];
function deleteitem(e){
let task = e.target.parentElement.previousElementSibling.innerHTML;
let index = tasklist.indexOf(task);
if(index!==-1){
tasklist.splice(index,1);
/*let months = ["January", "February", "Monday", "Tuesday"];
let days = months.splice(2, 1);
console.log(days); // ["Monday"]
console.log(months); // ["January", "February", "Tuesday"]*/
}
populatelist();
}
function populatelist(){
listelement.innerHTML='';
tasklist.forEach(function(item){
let newitem = document.createElement('li');
//adding enew span for text
let span = document.createElement('span');
span.innerHTML = item;
newitem.appendChild(span);
//add delete button
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));
//add Li to ul
listelement.appendChild(newitem); // Ul->Li->span->anchor
// span and anchor-> i
});
totaltaskselement.innerHTML=tasklist.length;
inputelement.value='';
}
populatelist();
function addtask(){
if(inputelement.value){
tasklist.push(inputelement.value);
populatelist();
}
}
formelement.addEventListener('submit',function(e){
e.preventDefault();
addtask();
});