-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
39 lines (32 loc) · 1.42 KB
/
search.js
File metadata and controls
39 lines (32 loc) · 1.42 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
// PSEUDO CODE : I.Au clic sur le bouton de la barre de recherche, je veux que soient affichés seulement les livres dont le titre correspond à la recherche. II. Mais je veux que les résultats soient réinitialisés si je fais une nouvelle recherche. III. Si l'input est vide, tous les résultats s'affichent de nouveau sans cliquer sur le bouton.
const searchFunction = () => {
const searchInput = document.querySelector("#search-input");
const searchButton = document.querySelector("#search-btn");
searchButton.addEventListener("click", (e) => {
// I.
const searchedByTheUser = searchInput.value.toLowerCase();
const allBooksTitles = document.querySelectorAll(".book-title");
document.startViewTransition(() => {
for (let bookTitle of allBooksTitles) {
const bookContainer = bookTitle.closest(".book-card");
if (!bookTitle.innerText.toLowerCase().includes(searchedByTheUser)) {
bookContainer.style.display = "none";
//II.
} else {
bookContainer.style.display = "block";
}
}
});
});
searchInput.addEventListener("input", () => {
const allBooksContainer = document.querySelectorAll(".book-card");
document.startViewTransition(() => {
if (searchInput.value.length === 0) {
for (let book of allBooksContainer) {
book.style.display = "block";
}
}
});
});
};
export default searchFunction;