-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.js
49 lines (43 loc) · 1.32 KB
/
dom.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
let form = document.getElementById('addForm')
let item = document.getElementById('items')
let search = document.getElementById('filter')
//form submit event
item.addEventListener('click', removeItem)
form.addEventListener('submit', addItem)
search.addEventListener('keyup', filterItem)
function addItem(e) {
e.preventDefault()
let newItem = document.getElementById('txtinput').value
if (newItem.length > 0) {
let li = document.createElement('li')
li.className = 'list-group-item d-flex'
let dltbtn = document.createElement('button')
dltbtn.className = 'btn btn-danger ms-auto btn-sm delete'
dltbtn.textContent = 'X'
li.appendChild(document.createTextNode(newItem))
li.appendChild(dltbtn)
item.appendChild(li)
document.getElementById('txtinput').value = ''
}
}
function removeItem(e) {
if (e.target.classList.contains('delete')) {
if (confirm('are u sure?')) {
let li = e.target.parentElement
item.removeChild(li)
}
}
}
function filterItem(e) {
let current = e.target.value.toLowerCase()
let stuff = item.getElementsByTagName('li')
Array.from(stuff).forEach(function (item) {
let itemName = item.firstChild.textContent.toLowerCase()
if (itemName.indexOf(current) != -1) {
item.classList.add('d-flex', 'list-group-item')
} else {
item.classList.remove('d-flex')
item.style.display = 'none'
}
})
}