-
Notifications
You must be signed in to change notification settings - Fork 2
/
shopCart.js
71 lines (48 loc) · 1.64 KB
/
shopCart.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
var shopCart = JSON.parse(localStorage.getItem("shopCart")) || [];
console.log(shopCart.length)
display();
function display(){
let t = document.getElementById("shopCart-count")
console.log(t)
shopCart.map(function(elem){
var div = document.createElement("div");
div.setAttribute("class","card");
div.setAttribute("id",elem.id);
var img = document.createElement("img");
img.setAttribute("src",elem.url);
var namev = document.createElement("p");
namev.setAttribute("class","name");
namev.textContent = elem.name;
var price = document.createElement("p");
price.setAttribute("class","price");
price.textContent = "₹"+" "+elem.price;
var rem = document.createElement("button");
rem.setAttribute("class","rem");
rem.textContent = "Remove";
rem.addEventListener("click",function(){
removeele(elem.id);
});
updateprice();
div.append(img,namev,price,rem);
document.getElementById("items").append(div);
})
}
function removeele(idx){
document.getElementById(idx).remove();
for(let i=0;i<shopCart.length;i++){
if(shopCart[i].id===idx){
shopCart.splice(i,1);
break;
}
}
updateprice();
document.getElementById("shopCart-count").textContent=shopCart.length;
localStorage.setItem("shopCart",JSON.stringify(shopCart));
}
function updateprice(){
var totalprice = 0;
shopCart.map(function(elem){
totalprice+=parseInt(elem.price);
})
document.getElementById("price").textContent="Price: "+"₹"+totalprice;
}