-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.js
69 lines (60 loc) · 2.2 KB
/
cart.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
61
62
63
64
65
66
67
68
69
let products = JSON.parse(localStorage.getItem("cart"));
function appendData(data) {
let totalPrice = 0;
for (let i = 0; i < data.length; i++) {
totalPrice += data[i].price * data[i].qty;
}
document.getElementById("cart-total").innerText = totalPrice;
document.getElementById("cart-container").innerHTML = "";
data.forEach((el, index) => {
let child = document.createElement("div");
let img = document.createElement("img");
img.src = el.img;
let brand = document.createElement("h2");
brand.innerText = el.brand;
let price = document.createElement("h3");
price.innerText = `₹${el.price}`;
let desc = document.createElement("p");
desc.innerText = el.details;
let category = document.createElement("p");
category.innerText = el.category;
let inc = document.createElement("button");
inc.innerText = "+";
inc.addEventListener("click", () => {
for (let i = 0; i < products.length; i++) {
if (products[i].id === el.id) {
products[i].qty++;
console.log(products[i]);
}
}
localStorage.setItem("cart", JSON.stringify(products));
appendData(products);
})
let span = document.createElement("span");
span.innerText = el.qty;
let dec = document.createElement("button");
dec.innerText = "-";
dec.addEventListener("click", () => {
for (let i = 0; i < products.length; i++) {
if (products[i].id === el.id) {
products[i].qty--;
console.log(products[i]);
}
}
localStorage.setItem("cart", JSON.stringify(products));
appendData(products);
})
let removeBtn = document.createElement("button");
removeBtn.innerText = "Remove";
removeBtn.addEventListener("click", () => {
data = data.filter((element, i) => {
return i !== index
})
localStorage.setItem("cart", JSON.stringify(data));
appendData(data);
})
child.append(img, brand, price, desc, category, inc, span, dec, removeBtn);
document.getElementById("cart-container").append(child);
});
}
appendData(products);