-
Notifications
You must be signed in to change notification settings - Fork 0
/
newDom.js
77 lines (72 loc) · 2.16 KB
/
newDom.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
let form = document.getElementById('addForm');
let itemList = document.getElementById('items');
let filters=document.getElementById('filter');
// Form submit event
form.addEventListener('submit', addItem);
// Delete event
itemList.addEventListener('click', removeItem);
//filter evet
filters.addEventListener('keyup', filterItems)
// Add item
function addItem(e){
e.preventDefault();
// Get input value
let newItem = document.getElementById('item').value;
let newDescription = document.getElementById('description').value;
// Create new li element
let li = document.createElement('li');
// Add class
li.className = 'list-group-item';
// Add text node with input value
li.appendChild(document.createTextNode(`${newItem} ${newDescription} `))
// Create del button element
let deleteBtn = document.createElement('button');
// Add classes to del button
deleteBtn.className = 'btn btn-danger btn-sm float-right delete';
// Append text node
deleteBtn.appendChild(document.createTextNode('X'));
// Append button to li
li.appendChild(deleteBtn);
//Add a edit button
let editBtn=document.createElement('button')
//add classs to edit button
editBtn.className ='btn btn-danger btn-sm edit';
//Append text node
editBtn.appendChild(document.createTextNode('edit'));
//Apend button to li
li.appendChild(editBtn);
// Append li to list
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);
}
}
if(e.target.classList.contains('edit')){
let li = e.target.parentElement;
let newText = prompt('Edit item:', li.firstChild.textContent);
if (newText !== null) {
li.firstChild.textContent = newText;
}
}
}
// filter item
function filterItems(e){
//convert the value in lowercase
let text=e.target.value.toLowerCase();
//get list
let items=itemList.getElementsByTagName('li');
//convert an array
Array.from(items).forEach(function(item){
let itemName=item.firstChild.textContent;
if(itemName.toLowerCase().indexOf(text) !== -1){
item.style.display='block';
} else{
item.style.display='none';
}
});
}