-
Notifications
You must be signed in to change notification settings - Fork 0
/
products.js
73 lines (58 loc) · 2.68 KB
/
products.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
/*
=========================================================
Name: products.js
Assignment 3
Authors: Joshua Maher (30148153), Yuecheng Sun (30180767)
Submission: Mar 11, 2024
=========================================================
*/
function addToCartAndDisplay(productName, productPrice) {
var existingItem = document.querySelector('.shopping-cart-items [data-name="' + productName + '"]');
if (existingItem) {
var quantityElement = existingItem.querySelector('.quantity');
var quantity = parseInt(quantityElement.textContent);
quantityElement.textContent = quantity + 1;
} else {
var cartItem = document.createElement('div');
cartItem.classList.add('cart-item');
cartItem.setAttribute('data-name', productName);
var itemName = document.createElement('span');
itemName.textContent = productName;
var itemPrice = document.createElement('span');
itemPrice.textContent = productPrice;
var itemQuantity = document.createElement('span');
itemQuantity.textContent = 1;
itemQuantity.classList.add('quantity');
var removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.classList.add('remove-item');
removeButton.addEventListener('click', function() {
var quantity = parseInt(itemQuantity.textContent);
if (quantity === 1) {
cartItem.remove();
} else {
itemQuantity.textContent = quantity - 1;
}
});
cartItem.appendChild(itemName);
cartItem.appendChild(document.createTextNode('\u00A0-\u00A0'));
cartItem.appendChild(itemPrice);
cartItem.appendChild(document.createTextNode('\u00A0-\u00A0'));
cartItem.appendChild(itemQuantity);
cartItem.appendChild(removeButton);
document.querySelector('.shopping-cart-items').appendChild(cartItem);
}
}
document.addEventListener('DOMContentLoaded', function() {
var addToCartButtons = document.querySelectorAll('.add-to-cart');
addToCartButtons.forEach(function(button) {
button.addEventListener('click', function(event) {
console.log("Add to cart button clicked");
var productDiv = event.target.closest('.product');
var productName = productDiv.querySelector('.product-name').textContent;
var productPrice = parseFloat(productDiv.querySelector('.product-price').textContent.replace('$', ''));
addToCartAndDisplay(productName, productPrice);
alert(productName + " has been added to the cart");
});
});
});