|
1 | 1 | const bookList = document.querySelector('#book-list ul'); |
2 | | -console.log(bookList); |
3 | 2 | const bookForm = document.forms['add-book']; |
4 | 3 | const hideBooks = document.querySelector('#hide'); |
5 | 4 | const bookRead = document.querySelectorAll('.book-complete'); |
| 5 | +const searchBooks = document.forms['search-books'].querySelector('input'); |
6 | 6 |
|
7 | 7 | //delete Function |
8 | | -bookList.addEventListener('click', function(e) { |
9 | | - if (e.target.matches('.delete')) { |
10 | | - deleteBooks(e.target); |
11 | | - } else if (e.target.matches('.book-complete')) { |
12 | | - completeRead(e.target); |
13 | | - } |
| 8 | +bookList.addEventListener('click', function (e) { |
| 9 | + if (e.target.matches('.delete')) { |
| 10 | + deleteBooks(e.target); |
| 11 | + } else if (e.target.matches('.book-complete')) { |
| 12 | + completeRead(e.target); |
| 13 | + } |
| 14 | +}); |
| 15 | + |
| 16 | +searchBooks.addEventListener('keyup', (e) => { |
| 17 | + const searchTerm = e.target.value.toLowerCase(); |
| 18 | + const books = bookList.querySelectorAll('li'); |
| 19 | + books.forEach((book) => { |
| 20 | + const title = book.querySelector('.name').textContent; |
| 21 | + if (title.toLowerCase().includes(searchTerm)) { |
| 22 | + book.style.display = 'block'; |
| 23 | + } else { |
| 24 | + book.style.display = 'none'; |
| 25 | + } |
| 26 | + }); |
14 | 27 | }); |
15 | 28 |
|
16 | 29 | //hide All books |
17 | 30 | hideBooks.addEventListener('change', (ev) => { |
18 | | - if (hideBooks.checked) { |
19 | | - bookList.style.display = 'none'; |
20 | | - } else { |
21 | | - bookList.style.display = 'initial'; |
22 | | - } |
| 31 | + if (hideBooks.checked) { |
| 32 | + bookList.style.display = 'none'; |
| 33 | + } else { |
| 34 | + bookList.style.display = 'initial'; |
| 35 | + } |
23 | 36 | }); |
24 | 37 |
|
25 | 38 | //delete books |
26 | 39 | function deleteBooks(ev) { |
27 | | - const li = ev.parentElement; |
28 | | - bookList.removeChild(li); |
| 40 | + const li = ev.parentElement; |
| 41 | + bookList.removeChild(li); |
29 | 42 | } |
30 | 43 |
|
31 | 44 | //complete read books |
32 | 45 | function completeRead(ev) { |
33 | | - ev.addEventListener('change', () => { |
34 | | - let title = ev.parentElement.querySelector('.name'); |
35 | | - if (ev.checked) { |
36 | | - title.style.textDecoration = 'line-through'; |
37 | | - } else { |
38 | | - title.style.textDecoration = 'initial'; |
39 | | - } |
40 | | - }); |
| 46 | + ev.addEventListener('change', () => { |
| 47 | + let title = ev.parentElement.querySelector('.name'); |
| 48 | + if (ev.checked) { |
| 49 | + title.style.textDecoration = 'line-through'; |
| 50 | + title.style.color = 'red'; |
| 51 | + } else { |
| 52 | + title.style.textDecoration = 'initial'; |
| 53 | + title.style.color = 'initial'; |
| 54 | + } |
| 55 | + }); |
41 | 56 | } |
42 | 57 |
|
43 | 58 | // add Book Function |
44 | 59 | bookForm.addEventListener('submit', (e) => { |
45 | | - e.preventDefault(); |
46 | | - let text = e.target.title.value; |
47 | | - let newLI = createAndAppendElement('li', bookList, null, null, undefined); |
48 | | - createAndAppendElement('input', newLI, null, 'book-complete', (e) => { |
49 | | - e.setAttribute('type', 'checkbox'); |
50 | | - }); |
51 | | - createAndAppendElement('label', newLI, null, null, (e) => { |
52 | | - e.htmlFor = checkBox.className; |
53 | | - }); |
54 | | - createAndAppendElement('span', newLI, null, 'name', (e) => { |
55 | | - e.textContent = text; |
56 | | - }); |
57 | | - createAndAppendElement('span', newLI, null, 'delete', (e) => { |
58 | | - e.textContent = 'delete'; |
59 | | - }); |
60 | | - e.target.title.value = ''; |
| 60 | + e.preventDefault(); |
| 61 | + let text = e.target.title.value; |
| 62 | + let newLI = createAndAppendElement('li', bookList, null, null, undefined); |
| 63 | + let checkBox = createAndAppendElement('input', newLI, null, 'book-complete', (e) => { |
| 64 | + e.setAttribute('type', 'checkbox'); |
| 65 | + }); |
| 66 | + createAndAppendElement('label', newLI, null, null, (e) => { |
| 67 | + e.htmlFor = checkBox.className; |
| 68 | + }); |
| 69 | + createAndAppendElement('span', newLI, null, 'name', (e) => { |
| 70 | + e.textContent = text; |
| 71 | + }); |
| 72 | + createAndAppendElement('span', newLI, null, 'delete', (e) => { |
| 73 | + e.textContent = 'delete'; |
| 74 | + }); |
| 75 | + e.target.title.value = ''; |
61 | 76 | }); |
62 | 77 |
|
63 | 78 | //helper append function to doc |
64 | 79 | function createAndAppendElement(tag, parent, id, className, callback) { |
65 | | - const element = document.createElement(tag); |
66 | | - parent.append(element); |
67 | | - if (id !== null) { |
68 | | - element.id = id; |
69 | | - } |
70 | | - if (className !== null) { |
71 | | - element.className = className; |
72 | | - } |
73 | | - if (callback !== undefined) { |
74 | | - callback(element); |
75 | | - } |
76 | | - return element; |
| 80 | + const element = document.createElement(tag); |
| 81 | + parent.append(element); |
| 82 | + if (id !== null) { |
| 83 | + element.id = id; |
| 84 | + } |
| 85 | + if (className !== null) { |
| 86 | + element.className = className; |
| 87 | + } |
| 88 | + if (callback !== undefined) { |
| 89 | + callback(element); |
| 90 | + } |
| 91 | + return element; |
77 | 92 | } |
0 commit comments