-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
47 lines (40 loc) · 1.38 KB
/
search.js
File metadata and controls
47 lines (40 loc) · 1.38 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
40
41
42
43
44
45
46
47
let container = document.querySelector("#products")
let searchResult = document.querySelector(".search-result")
let clearResult = document.querySelector(".clear-result")
const params = new URLSearchParams(window.location.search)
const query = params.get('q')
searchResult.textContent = `search results for "${query}"`
document.title = `Search: ${query} | Aditya Kirana Store`;
const filteredProducts = products.filter(
p => p.name.toLowerCase().includes (query.toLowerCase())
)
if(filteredProducts.length > 0){
filteredProducts.forEach(p=> {
container.innerHTML += `
<div class="product-card" data-id="${p.id}">
<a href="product.html?id=${p.id}" class="product-link">
<img src="${p.image}" alt="${p.name}">
<h4 class = "category-h4">${p.name}</h4>
<p class="qty">${p.quantity}</p>
<p class="price"><del>₹${p.mrp}</del> ₹${p.price}</p>
</a>
<button class="add-to-cart">Add to Cart</button>
</div>
`;
});
}
else{
searchResult.textContent = `No products found for "${query}" 😢`
}
clearResult.addEventListener("click",()=>{
window.location.href = "index.html"
})
container.addEventListener("click",(e)=>{
if(!e.target.classList.contains("add-to-cart")) return
e.preventDefault()
e.stopPropagation()
const card = e.target.closest(".product-card")
const id = card.dataset.id
const product = products.find(p=> p.id == id)
addToCart(product)
})