-
Notifications
You must be signed in to change notification settings - Fork 487
Expand file tree
/
Copy pathapp.js
More file actions
46 lines (34 loc) · 1.31 KB
/
app.js
File metadata and controls
46 lines (34 loc) · 1.31 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
// const list = document.querySelector('#book-list ul');
// // delete books
// list.addEventListener('click', (e) => {
// if(e.target.className == 'delete'){
// const li = e.target.parentElement;
// li.parentNode.removeChild(li);
// }
// });
const list = document.querySelector('#book-list ul');
//delete books
list.addEventListener('click', function(e){
if(e.target.className == 'delete'){
const li = e.target.parentElement;
li.parentNode.removeChild(li);
}
});
const addForm = document.forms['add-book'];
addForm.addEventListener('submit', function (e) {
e.preventDefault();
const value = addForm.querySelector('input[type="text"]').value;
//create elements
const li = document.createElement('li'); //not input inside the dom yet, only created
const bookName = document.createElement('span');
const deleteBtn = document.createElement('span');
//append to Document
li.appendChild(bookName); //order does matter here
li.appendChild(deleteBtn);
//append the list to the unorderd list (ul) in the dom
list.appendChild(li) //the ul was selected using queryselected and asigned to list
//add textcontent to the bookName & deletebutton
deleteBtn.textContent = 'delete';
bookName.textContent = value;
deleteBtn.className = 'delete'
})