-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
60 lines (48 loc) · 2.04 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
50
51
52
53
54
55
56
57
58
59
60
const bagCount = document.getElementById('bag-count');
const addToBagButtons = document.querySelectorAll('.add-to-bag');
const filterButtons = document.querySelectorAll('.filter-btn');
const products = document.querySelectorAll('.product');
// Load the bag from localStorage or initialize an empty array
let bag = JSON.parse(localStorage.getItem('bag')) || [];
// Update the bag count on page load
bagCount.textContent = bag.length;
addToBagButtons.forEach(button => {
button.addEventListener('click', () => {
const product = button.closest('.product');
const productTitle = product.querySelector('.product-title').textContent;
const productPrice = product.querySelector('.product-price').textContent.replace('₹', '').trim();
// Check if the product is already in the bag
const existingItem = bag.find(item => item.title === productTitle);
if (existingItem) {
existingItem.quantity += 1; // Increment quantity if already in the bag
} else {
// Add new item to the bag
bag.push({
title: productTitle,
price: productPrice,
quantity: 1
});
}
// Update bag count
bagCount.textContent = bag.length;
// Store the updated bag in localStorage
localStorage.setItem('bag', JSON.stringify(bag));
alert(`${productTitle} has been added to your bag!`);
});
});
filterButtons.forEach(button => {
button.addEventListener('click', () => {
const category = button.getAttribute('data-category');
products.forEach(product => {
if (category === 'all' || product.classList.contains(category)) {
product.style.display = 'block';
} else {
product.style.display = 'none';
}
});
// Remove active class from all buttons
filterButtons.forEach(btn => btn.classList.remove('active'));
// Add active class to the clicked button
button.classList.add('active');
});
});