-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
82 lines (63 loc) · 2.4 KB
/
script.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
78
79
80
81
82
const list = document.querySelector("#book-list ul");
const addButton = document.querySelector("#add-book :nth-child(2) button");
const hide = document.querySelector("#hide");
const searchBooks = document.querySelector('#search-box input');
// start --------------- delete books
list.addEventListener('click', function(e) {
if (e.target.className == 'delete') {
const li = e.target.parentElement;
li.parentNode.removeChild(li);
}
});
// start --------------- add books
addButton.addEventListener('click', function(e) {
e.preventDefault();
// 1. create elements
const li = document.createElement('li');
const span = document.createElement('span');
const button = document.createElement('button');
let addInput = document.querySelector("#add-book :nth-child(2) input[type='text']");
// 3. add text to the creaated elements
span.innerText = addInput.value;
button.innerText = 'Delete';
// ! if text is empty then noting will happen
if (span.innerText != '') {
// 2. add classes
span.className = 'name';
button.className = 'delete';
// 4. append / add
li.appendChild(span);
li.appendChild(button);
// 5. finally add it to the ul list
list.appendChild(li);
// 6. after taking the 'book' name from the input, then the input feild again blank the input
addInput.value = '';
} else {
alert("Please add a Book Name");
}
})
// start ------------ 'hide' books
hide.addEventListener('change', function() {
if (hide.checked) {
list.style.display = 'none';
} else {
list.style.display = 'block';
}
})
// start ------------ 'search' books / filter books
searchBooks.addEventListener('keyup', function(e) {
// 1. grab the input value from user
const term = e.target.value.toLowerCase();
// 2. grab all the 'li' tags
const books = list.getElementsByTagName('li');
// 3. cycle/loop through those 'li' tag to get matched items, so turn 'books' into an array
Array.from(books).forEach(function(book) {
const bookName = book.firstElementChild.innerText; // .text-content
// 4. now compare the book Name with user input (-1 => not ok)
if (bookName.toLowerCase().indexOf(term) != -1) {
book.style.display = 'block';
} else {
book.style.display = 'none';
}
})
})