-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
33 lines (26 loc) · 871 Bytes
/
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
let _index = 0;
let listContainer = document.getElementById('ul')
document.getElementById('add').onclick = () => {
let _v = document.getElementById('text').value
if (!_v) {
alert("Input text!")
return;
}
createListItem(_v)
document.getElementById('text').value = ''
}
const createListItem = (_v) => {
let _item = document.createElement('li')
_item.setAttribute('id', `li-${_index}`)
let delbtn = document.createElement('button')
delbtn.className = 'del-btn'
delbtn.appendChild(document.createTextNode('Delete'))
_item.appendChild(document.createTextNode(_v))
delbtn.setAttribute('data-id', _index)
_item.appendChild(delbtn)
listContainer.appendChild(_item)
_index++
delbtn.onclick = function () {
document.getElementById(`li-${this.getAttribute('data-id')}`).remove()
}
}