-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.js
123 lines (99 loc) · 3.46 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
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
117
118
119
120
121
122
123
var cartarr=JSON.parse(localStorage.getItem("cartdata"))
console.log(cartarr)
displaycart(cartarr)
var tprice=0;
function displaycart(arr){
arr.forEach(function(elem,index){
var box=document.createElement("div")
box.setAttribute("id","box")
var div1=document.createElement("div")
div1.setAttribute("id","deleteicon")
var delimg= document.createElement("img");
delimg.setAttribute("src","https://cdn-icons-png.flaticon.com/128/1345/1345823.png");
delimg.addEventListener("click",function(){
deleteitem(elem,index);
})
div1.append(delimg)
var div2=document.createElement("div")
div2.setAttribute("id","productimg")
var pimg= document.createElement("img");
pimg.setAttribute("src", elem.avatar);
div2.append(pimg)
var div3=document.createElement("div")
div3.setAttribute("id","productname")
var pnames=document.createElement("p")
pnames.innerText=elem.name
div3.append(pnames)
var div4=document.createElement("div")
div4.setAttribute("id","price")
var pprice=document.createElement("p")
pprice.innerText=elem.position
div4.append(pprice)
var div5=document.createElement("div")
div5.setAttribute("id","incdec")
var inc=document.createElement("button")
inc.setAttribute("id","inc")
inc.innerText="+"
var dec=document.createElement("button")
dec.setAttribute("id","dec")
dec.innerText="-"
var x = document.createElement("input");
x.setAttribute("id","qntbox")
x.setAttribute("type", "text");
x.setAttribute("value", "1");
div5.append(dec,x,inc)
var div6=document.createElement("div")
div6.setAttribute("id","totalprice")
var incprice=+(elem.position.replace("$",""))
console.log(incprice)
console.log(sum)
div6.append(incprice)
inc.addEventListener('click', () => {
x.value = Number(x.value) + 1;
div6.innerHTML="";
tprice=incprice*x.value
div6.append(tprice)
});
dec.addEventListener('click', () => {
if (x.value <= 1) {
x.value = 1;
div6.innerHTML="";
tprice=incprice*x.value
div6.append(tprice)
}
else {
x.value = Number(x.value) - 1
div6.innerHTML="";
tprice=incprice*x.value
div6.append(tprice)
}
});
box.append(div1,div2,div3,div4,div5,div6)
document.querySelector("#cartlist").append(box)
})
}
function deleteitem(elem,index){
cartarr.splice(index,1);
localStorage.setItem("cartdata",JSON.stringify(cartarr))
window.location.reload();
}
var sum= cartarr.reduce(function(acc,elem){
return acc+Number(elem.position.replace("$",""))
},0)
var h2=document.createElement("h2")
h2.innerText="Subtotal: $"+sum
var h1=document.createElement("h1")
h1.innerText="Grand Total: $"+sum
document.querySelector("#sumbox").append(h2,h1)
console.log(sum)
// function addbtn(e) {
// e = parseInt(qtn.value) + 1;
// };
// subBtn.addEventListener('click', () => {
// if (qty.value <= 1) {
// qty.value = 1;
// }
// else {
// qty.value = parseInt(qty.value) - 1
// }
// });