Skip to content

Commit 8a0da74

Browse files
committed
test
1 parent 0c1ea1b commit 8a0da74

File tree

1 file changed

+65
-50
lines changed

1 file changed

+65
-50
lines changed

app.js

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,92 @@
11
const bookList = document.querySelector('#book-list ul');
2-
console.log(bookList);
32
const bookForm = document.forms['add-book'];
43
const hideBooks = document.querySelector('#hide');
54
const bookRead = document.querySelectorAll('.book-complete');
5+
const searchBooks = document.forms['search-books'].querySelector('input');
66

77
//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+
});
1427
});
1528

1629
//hide All books
1730
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+
}
2336
});
2437

2538
//delete books
2639
function deleteBooks(ev) {
27-
const li = ev.parentElement;
28-
bookList.removeChild(li);
40+
const li = ev.parentElement;
41+
bookList.removeChild(li);
2942
}
3043

3144
//complete read books
3245
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+
});
4156
}
4257

4358
// add Book Function
4459
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 = '';
6176
});
6277

6378
//helper append function to doc
6479
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;
7792
}

0 commit comments

Comments
 (0)