-
Notifications
You must be signed in to change notification settings - Fork 1
/
Search.js
55 lines (45 loc) · 1.8 KB
/
Search.js
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
48
49
50
51
52
53
54
55
let mainContainer = document.getElementById("adiv");
// Function to display search results
function displayResults(results) {
// Clear previous search results
mainContainer.innerHTML = '';
// Display search results
results.forEach(product => {
const productCard = document.createElement('div');
productCard.classList.add('product-card_h');
const productImage = document.createElement('img');
productImage.src = product.img;
productImage.alt = product.title;
productCard.appendChild(productImage);
const title = document.createElement('h3');
title.textContent = product.title;
productCard.appendChild(title);
const weight = document.createElement('p');
weight.textContent = product.weight;
productCard.appendChild(weight);
const price = document.createElement('p');
price.textContent = product.price;
productCard.appendChild(price);
const btn = document.createElement('button');
btn.textContent = "Add to Cart"; // Change button text if needed
btn.classList.add('A-btn');
productCard.appendChild(btn);
mainContainer.appendChild(productCard);
});
}
// Fetch data from the server
fetch('https://betwa-interface-015.onrender.com/cards')
.then(response => response.json())
.then(data => {
const products = data;
// Event listener for search input
let searchbar = document.getElementById("Search_h");
searchbar.addEventListener("input", () => {
const searchTerm = searchbar.value.toLowerCase();
const filteredProducts = products.filter(product => {
return product.title.toLowerCase().includes(searchTerm);
});
displayResults(filteredProducts);
});
})
.catch(error => console.error('Error fetching data:', error));