-
Notifications
You must be signed in to change notification settings - Fork 2
/
cartPage.js
69 lines (64 loc) · 2.5 KB
/
cartPage.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
const bookmarkData = JSON.parse(localStorage.getItem('bookmark'));
const tbody = document.querySelector("tbody");
const updateTP = document.querySelector("#updateTP");
const updateButton = document.querySelector("#rightCart>button");
var tp = 0;
displayCartData();
function displayCartData(){
tbody.innerHTML = null;
if(bookmarkData==null){
let thead = document.querySelector('#cartBox>div');
thead.innerHTML = 'Your cart is empty!!';
}
else{
bookmarkData.forEach(element => {
let tr = document.createElement('tr');
let td1 = document.createElement('td');
let img = document.createElement('img');
img.setAttribute('id','cartIMG');
img.src = element.productIMG;
let td2 = document.createElement('td');
td2.innerText = element.productName;
let td3 = document.createElement('td');
let qty = document.createElement('input');
qty.type = 'text';
td3.setAttribute('id','inputQTY');
qty.value = element.productQTY;
let td4 = document.createElement('td');
let totalPrice = Number(element.productPrice) * Number(element.productQTY);
td4.innerText = 'RS. '+ totalPrice+'.00';
tp += totalPrice;
updateTP.textContent = 'RS. '+ tp +'.00'
td1.append(img);
td3.append(qty);
tr.append(td1,td2,td3,td4);
tbody.append(tr);
});
}
}
updateButton.addEventListener('click', function(){
// let updateQTY = document.querySelector("#inputQTY");
// console.log(updateQTY);
// bookmarkData.forEach(element => {
// // element.productQTY = 2;
// // console.log(element.productQTY);
// });
tp = 0;
displayCartData();
updateTP.textContent = 'RS. '+ tp +'.00';
});
function addToCart(){
//add to cart
var bookmarkData = JSON.parse(localStorage.getItem('bookmark')) || [];
addToCart.addEventListener('click',function(){
bookmarkObj = {
productIMG : document.getElementById('productIMG').src,
productPrice : document.querySelector('#productPrice>span').innerText,
productQTY : document.getElementById('productQTY').innerText,
productName : document.getElementById('productName').innerText
}
bookmarkData.push(bookmarkObj);
localStorage.setItem('bookmark',JSON.stringify(bookmarkData));
console.log(bookmarkData);
});
}