-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
56 lines (38 loc) · 1.12 KB
/
main.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
var form = document.getElementById('addForm');
var itemList = document.getElementById('items')
// Form submit event
form.addEventListener('submit', addItem);
//Delete Event
itemList.addEventListener('click', removeItem);
// Add item
function addItem(e){
e.preventDefault();
//console.log(1);
//Get input value;
var newItem = document.getElementById('item').value;
//create new li element
var li = document.createElement('li');
//Add Class
li.className = 'list-group-item'
//Add TextNode
li.appendChild(document.createTextNode(newItem));
//Add delete button
var deleteBtn = document.createElement('button');
//Add Class
deleteBtn.className = 'btn btn-danger btn-sm float-right delete'
//Add TextNode
deleteBtn.appendChild(document.createTextNode('X'));
//Append Buttn to li
li.appendChild(deleteBtn);
//Append li to itemList
itemList.appendChild(li);
}
// Remove item
function removeItem(e){
if(e.target.classList.contains('delete')){
if(confirm('Are You Sure?')){
var li = e.target.parentElement;
itemList.removeChild(li);
}
}
}