Repository navigation
Expand file tree
/
Copy pathscripts.js
More file actions
40 lines (33 loc) · 1.38 KB
/
Copy pathscripts.js
File metadata and controls
40 lines (33 loc) · 1.38 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
document.addEventListener('DOMContentLoaded', () => {
const cartList = document.getElementById('cart-list');
const cartTotal = document.getElementById('cart-total');
let totalCost = 0;
let totalItems = 0;
document.querySelectorAll('.add-to-bundle').forEach(button => {
button.addEventListener('click', (event) => {
const card = event.target.closest('.challenge-card');
const title = card.dataset.title;
const cost = parseFloat(card.dataset.cost);
const listItem = document.createElement('li');
listItem.textContent = title;
cartList.appendChild(listItem);
totalCost += cost;
totalItems += 1;
cartTotal.textContent = totalCost.toFixed(2);
const cartIcon = document.querySelector('.cart a');
cartIcon.textContent = `Cart (${totalItems})`;
});
});
document.getElementById('checkout-button').addEventListener('click', () => {
const cartItems = [];
document.querySelectorAll('#cart-list li').forEach(item => {
cartItems.push(item.textContent);
});
const cartData = {
items: cartItems,
totalCost: totalCost
};
localStorage.setItem('cartData', JSON.stringify(cartData));
window.location.href = 'order-summary.html';
});
});