-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.html
More file actions
116 lines (98 loc) · 4.1 KB
/
test.html
File metadata and controls
116 lines (98 loc) · 4.1 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
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dynamic Cart</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen py-8 px-4 text-gray-800">
<div class="max-w-5xl mx-auto">
<h1 class="text-3xl font-bold mb-6 text-center">🛒 Your Cart</h1>
<!-- Cart Container -->
<div id="cart-container" class="space-y-4"></div>
<!-- Totals -->
<div class="mt-8 bg-white shadow-md rounded-lg p-6 text-right">
<p class="text-xl">Subtotal: <span id="subtotal" class="font-bold">$0.00</span></p>
<p class="text-xl">Tax (10%): <span id="tax" class="font-bold">$0.00</span></p>
<p class="text-2xl mt-2">Total: <span id="total" class="font-bold text-green-600">$0.00</span></p>
</div>
</div>
<script>
const cartItems = [
{
id: 1,
name: "Product A",
price: 20.0,
quantity: 1,
image: "https://via.placeholder.com/100"
},
{
id: 2,
name: "Product B",
price: 35.0,
quantity: 2,
image: "https://via.placeholder.com/100"
}
];
const taxRate = 0.1;
function renderCart() {
const cartContainer = document.getElementById("cart-container");
cartContainer.innerHTML = ""; // Clear previous content
cartItems.forEach((item, index) => {
const itemHTML = `
<div class="bg-white shadow rounded-lg p-4 flex items-center justify-between">
<div class="flex items-center gap-4">
<img src="${item.image}" alt="${item.name}" class="w-20 h-20 object-cover rounded">
<div>
<h2 class="font-semibold text-lg">${item.name}</h2>
<p class="text-gray-500">Price: $${item.price.toFixed(2)}</p>
</div>
</div>
<div class="flex items-center gap-4">
<button data-index="${index}" class="decreaseQty bg-red-500 text-white px-2 py-1 rounded">-</button>
<span id="qty-${index}" class="min-w-[24px] text-center">${item.quantity}</span>
<button data-index="${index}" class="increaseQty bg-green-500 text-white px-2 py-1 rounded">+</button>
<p class="font-bold w-24 text-right" id="price-${index}">$${(item.price * item.quantity).toFixed(2)}</p>
</div>
</div>
`;
cartContainer.insertAdjacentHTML("beforeend", itemHTML);
});
attachEventListeners();
updateTotals();
}
function attachEventListeners() {
document.querySelectorAll(".increaseQty").forEach((btn) => {
btn.addEventListener("click", () => {
const index = parseInt(btn.dataset.index);
cartItems[index].quantity++;
renderCart();
});
});
document.querySelectorAll(".decreaseQty").forEach((btn) => {
btn.addEventListener("click", () => {
const index = parseInt(btn.dataset.index);
if (cartItems[index].quantity > 1) {
cartItems[index].quantity--;
renderCart();
}
});
});
}
function updateTotals() {
let subtotal = 0;
cartItems.forEach((item) => {
subtotal += item.price * item.quantity;
});
const tax = subtotal * taxRate;
const total = subtotal + tax;
document.getElementById("subtotal").textContent = `$${subtotal.toFixed(2)}`;
document.getElementById("tax").textContent = `$${tax.toFixed(2)}`;
document.getElementById("total").textContent = `$${total.toFixed(2)}`;
}
// Initial render
renderCart();
</script>
</body>
</html>