Skip to content

18p Solution #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 18p
Choose a base branch
from
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');
Copy link

@antaugustol antaugustol Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are only using the search part of the url, we could do it like this:

  const url = new URLSearchParams(location.search)
  const search = url.get('search').toLowerCase()


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}`;
});
}