-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy.js
More file actions
113 lines (97 loc) · 4.2 KB
/
my.js
File metadata and controls
113 lines (97 loc) · 4.2 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
document.addEventListener("DOMContentLoaded", () => {
// Variables for DOM elements
const cartModal = document.getElementById("cart-modal");
const closeButton = document.querySelector(".close-button");
const cartCount = document.getElementById("cart-count");
const cartItems = document.getElementById("cart-items");
const totalPriceElement = document.getElementById("total-price");
let cart = [];
// Event listener for 'Add to Cart' buttons
document.querySelectorAll(".add-to-cart").forEach(button => {
button.addEventListener("click", () => {
const productElement = button.parentElement;
const productId = productElement.getAttribute("data-id");
const productName = productElement.getAttribute("data-name");
const productPrice = parseInt(productElement.getAttribute("data-price").replace(/,/g, ''));
addToCart(productId, productName, productPrice);
});
});
// Function to add items to cart
function addToCart(id, name, price) {
const existingItem = cart.find(item => item.id === id);
if (existingItem) {
existingItem.quantity += 1;
} else {
cart.push({ id, name, price, quantity: 1 });
}
updateCartDisplay();
}
// Update cart display and total price
function updateCartDisplay() {
cartCount.textContent = cart.reduce((total, item) => total + item.quantity, 0);
cartItems.innerHTML = "";
let total = 0;
cart.forEach(item => {
const itemElement = document.createElement("div");
itemElement.className = "cart-item";
itemElement.innerHTML = `
<span>${item.name} (x${item.quantity}) - Rs ${item.price}</span>
<button onclick="removeItem('${item.id}')">Remove</button>
`;
cartItems.appendChild(itemElement);
total += item.price * item.quantity;
});
totalPriceElement.textContent = total;
}
// Remove item from cart
window.removeItem = function(id) {
cart = cart.filter(item => item.id !== id);
updateCartDisplay();
};
// Open and close cart modal
cartModal.style.display = "none";
document.getElementById("cart").addEventListener("click", () => {
cartModal.style.display = "block";
});
closeButton.addEventListener("click", () => {
cartModal.style.display = "none";
});
window.addEventListener("click", (event) => {
if (event.target == cartModal) {
cartModal.style.display = "none";
}
});
// Filter products by price and category
document.getElementById("apply-filter").addEventListener("click", () => {
const maxPrice = parseInt(document.getElementById("price-filter").value) || Infinity;
const category = document.getElementById("category-filter").value;
document.querySelectorAll(".product").forEach(product => {
const price = parseInt(product.getAttribute("data-price").replace(/,/g, ''));
const productCategory = product.getAttribute("data-category");
if (price <= maxPrice && (category === "all" || category === productCategory)) {
product.style.display = "block";
} else {
product.style.display = "none";
}
});
});
// Reset filters
document.getElementById("reset-filter").addEventListener("click", () => {
document.getElementById("price-filter").value = "";
document.getElementById("category-filter").value = "all";
document.querySelectorAll(".product").forEach(product => {
product.style.display = "block";
});
});
// Checkout button event
document.getElementById("checkout").addEventListener("click", () => {
if (cart.length === 0) {
alert("Your cart is empty!");
} else {
alert("Proceeding to checkout. Total: Rs " + totalPriceElement.textContent);
cart = [];
updateCartDisplay();
cartModal.style.display = "none";
}
});
});