forked from janavipandole/Cara
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout.js
More file actions
70 lines (45 loc) · 1.44 KB
/
Copy pathcheckout.js
File metadata and controls
70 lines (45 loc) · 1.44 KB
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
const paymentMethod = document.getElementById("paymentMethod");
const cardDetails = document.getElementById("cardDetails");
const cardName = document.getElementById("cardName");
const cardNumber = document.getElementById("cardNumber");
const expiry = document.getElementById("expiry");
const cvv = document.getElementById("cvv");
// SHOW / HIDE CARD DETAILS
paymentMethod.addEventListener("change", function () {
if (this.value === "online") {
cardDetails.style.display = "block";
cardName.required = true;
cardNumber.required = true;
expiry.required = true;
cvv.required = true;
} else {
cardDetails.style.display = "none";
cardName.required = false;
cardNumber.required = false;
expiry.required = false;
cvv.required = false;
}
});
// FORM SUBMIT
const form = document.getElementById("checkoutForm");
const popup = document.getElementById("successPopup");
form.addEventListener("submit", function (e) {
e.preventDefault();
// GET CART
let cart = JSON.parse(localStorage.getItem("productsInCart")) || [];
// CHECK EMPTY CART
if (cart.length === 0) {
alert("Your cart is empty!");
return;
}
// CLEAR CART AFTER SUCCESSFUL ORDER
localStorage.removeItem("productsInCart");
// SHOW SUCCESS POPUP
popup.classList.add("active");
form.reset();
// HIDE CARD DETAILS AGAIN
cardDetails.style.display = "none";
});
function closePopup() {
popup.classList.remove("active");
}