-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
49 lines (43 loc) · 1.9 KB
/
script.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
// Function to load products dynamically from JSON file
async function loadProductsFromJSON() {
const response = await fetch('products.json');
const products = await response.json();
const productsContainer = document.getElementById('productsContainer');
productsContainer.innerHTML = ''; // Clear the existing content
products.forEach(product => {
const productElement = document.createElement('div');
productElement.classList.add('product');
productElement.innerHTML = `
<img src="${product.image_url}" alt="${product.name}">
<h2>${product.name}</h2>
<a href="${product.affiliate_link}" target="_blank">View</a>
`;
productsContainer.appendChild(productElement);
});
}
// Search functionality
document.getElementById('searchBox').addEventListener('input', function () {
const searchTerm = this.value.toLowerCase();
fetch('products.json')
.then(response => response.json())
.then(products => {
const filteredProducts = products.filter(product =>
product.name.toLowerCase().includes(searchTerm)
);
document.getElementById('productsContainer').innerHTML = '';
filteredProducts.forEach(product => {
const productElement = document.createElement('div');
productElement.classList.add('product');
productElement.innerHTML = `
<img src="${product.image_url}" alt="${product.name}">
<h2>${product.name}</h2>
<a href="${product.affiliate_link}" target="_blank">View</a>
`;
document.getElementById('productsContainer').appendChild(productElement);
});
});
});
// Load products when the page loads
window.onload = () => {
loadProductsFromJSON();
};