Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 2-copy-of-code/lesson-18/amazon.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
</div>

<div class="amazon-header-middle-section">
<input class="search-bar" type="text" placeholder="Search">
<input class="search-bar js-search-bar" type="text" placeholder="Search">

<button class="search-button">
<button class="search-button js-search-button">
<img class="search-icon" src="images/icons/search-icon.png">
</button>
</div>
Expand Down
23 changes: 21 additions & 2 deletions 2-copy-of-code/lesson-18/scripts/amazon.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ loadProducts(renderProductsGrid);
function renderProductsGrid() {
let productsHTML = '';

products.forEach((product) => {
const url = new URL(window.location.href);
const search = url.searchParams.get('search');

let filteredProducts = products;

// If a search exists in the URL parameters,
// filter the products that match the search.
if (search) {
filteredProducts = products.filter((product) => {
return product.name.includes(search);
});
}

filteredProducts.forEach((product) => {
productsHTML += `
<div class="product-container">
<div class="product-image-container">
Expand Down Expand Up @@ -84,4 +97,10 @@ function renderProductsGrid() {
updateCartQuantity();
});
});
}

document.querySelector('.js-search-button')
.addEventListener('click', () => {
const search = document.querySelector('.js-search-bar').value;
window.location.href = `amazon.html?search=${search}`;
});
}