-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.js
58 lines (41 loc) · 1.28 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
let cartArr = JSON.parse(localStorage.getItem("cart"))||[]
function display(array){
document.querySelector("#product").innerHTML=""
array.forEach(function(element,index) {
let div = document.createElement("div")
let img = document.createElement("img")
img.setAttribute("src", element.img)
let h4 = document.createElement("h4")
h4.innerText = element.name
let h3 = document.createElement("h3")
h3.innerText = element.price
let p = document.createElement("p")
p.innerText = element.desc
let btn = document.createElement("button")
btn.innerText = "Remove"
btn.addEventListener("click",function(){
dlData(cartArr,index)
refresh()
})
div.append(img,h4,h3,p,btn)
document.querySelector("#product").append(div)
});
}
display(cartArr)
function dlData(cartArr, index){
cartArr.splice(index,1)
localStorage.setItem("cart",JSON.stringify(cartArr))
display(cartArr)
}
function refresh(){
let total = cartArr.reduce(function(acc,el){
return acc + el.price
},0)
console.log(total)
document.querySelector("span").innerText=total
}
let total = cartArr.reduce(function(acc,el){
return acc + el.price
},0)
console.log(total)
document.querySelector("span").innerText=total