Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/morrismagic/html1


https://github.com/morrismagic/html1

Last synced: 1 day ago
JSON representation

Awesome Lists containing this project

README

        




Shopping Cart



Item 1


-
1
+
❤️
Remove
$20


Item 2


-
1
+
❤️
Remove
$15

Total: $35


document.addEventListener('DOMContentLoaded', () => {
const cartItems = document.querySelectorAll('.cart-item');

cartItems.forEach(item => {
const priceElement = item.querySelector('.price');
const quantityElement = item.querySelector('.quantity');
const increaseButton = item.querySelector('.increase');
const decreaseButton = item.querySelector('.decrease');
const removeButton = item.querySelector('.remove');
const likeButton = item.querySelector('.like');

increaseButton.addEventListener('click', () => {
let quantity = parseInt(quantityElement.textContent);
quantity++;
quantityElement.textContent = quantity;
updateTotal();
});

decreaseButton.addEventListener('click', () => {
let quantity = parseInt(quantityElement.textContent);
if (quantity > 1) {
quantity--;
quantityElement.textContent = quantity;
updateTotal();
}
});

removeButton.addEventListener('click', () => {
item.remove();
updateTotal();
});

likeButton.addEventListener('click', () => {
likeButton.classList.toggle('liked');
likeButton.textContent = likeButton.classList.contains('liked') ? '❤️' : '🤍';
});
});

function updateTotal() {
let total = 0;
cartItems.forEach(item => {
const price = parseFloat(item.dataset.price);
const quantity = parseInt(item.querySelector('.quantity').textContent);
total += price * quantity;
});
document.getElementById('total').textContent = `Total: $${total}`;
}
});

body {
font-family: Arial, sans-serif;
}

#cart {
width: 300px;
margin: auto;
}

.cart-item {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
}

button {
margin: 0 5px;
}

.liked {
color: red;
}