I'm very new to jQuery and I managed to make this code work but I'm pretty sure that it could be made in a more efficient way. For example, it should be a way to send the id as a parameter instead of writing the same code for each id.
But beside of that, are there other things that could be made in a better way?
// selecting elements
$('#0')
.find('.cart-quantity-input')
.change(function () {
var quantity = parseFloat($('#0').find('.cart-quantity-input').val());
var price = parseFloat(
$('#0').find('.cart-price').text().replace('€', '')
);
$('#0')
.find('.cart-subtotal')
.text(quantity * price + '€');
});
$('#1')
.find('.cart-quantity-input')
.change(function () {
var quantity = parseFloat($('#1').find('.cart-quantity-input').val());
var price = parseFloat(
$('#1').find('.cart-price').text().replace('€', '')
);
$('#1')
.find('.cart-subtotal')
.text(quantity * price + '€');
});
$('#2')
.find('.cart-quantity-input')
.change(function () {
var quantity = parseFloat($('#2').find('.cart-quantity-input').val());
var price = parseFloat(
$('#2').find('.cart-price').text().replace('€', '')
);
$('#2')
.find('.cart-subtotal')
.text(quantity * price + '€');
});
// compute subtotal sum
function subTotal(element) {
var quantity = element.siblings('.cart-quantity-input').val();
var price = parseFloat(
element.closest('.cart-row').find('.cart-price').text().replace('€', '')
);
$(element)
.closest('.cart-row')
.find('.cart-subtotal')
.text(price * quantity + '€');
}
// compute total sum and number of items
function computeTotalValues() {
var total = [...$('.cart-items .cart-subtotal')]
.map((subtotalElm) => Number(subtotalElm.textContent.replace('€', '')))
.reduce((a, b) => a + b, 0);
$('.cart-total-price').text(total + '€');
const totalQuantity = $.map(
$('.cart-items .cart-quantity-input'),
(input) => +input.value
).reduce((a, b) => a + b, 0);
$('.items-number').text(totalQuantity + ' items');
}
// increase number of items
$('.plus').on('click', function () {
var increment = parseInt($(this).siblings('input').val());
increment++;
$(this).siblings('input').val(increment);
subTotal($(this));
computeTotalValues();
});
// decrease number of items
$('.minus').on('click', function () {
var decrement = parseInt($(this).siblings('input').val());
if (decrement) {
decrement--;
}
$(this).siblings('input').val(decrement);
subTotal($(this));
computeTotalValues();
});
// checkout button clicked
$('.btn-primary').on('click', function () {
if (
confirm('Are you sure you want to checkout? \nYour products will disappear')
) {
$('.cart-total-price').text('0 €');
$('.items-number').text('0 items');
$('.cart-quantity-input').val('0');
}
});
* {
box-sizing: border-box;
font-family: sans-serif;
color: #777;
}
html,
body {
margin: 0;
padding: 0;
min-height: 100%;
background-color: #261741;
}
.container {
display: flex;
width: 55%;
min-width: 700px;
margin: 0 auto;
border-radius: 10px;
overflow: hidden;
margin-top: 50px;
min-height: 500px;
}
.first-section {
background-color: #ffffff;
position: relative;
width: 70%;
padding-left: 40px;
padding-top: 10px;
}
.second-section {
background-color: #f3f3f3;
position: relative;
width: 30%;
padding: 10px 20px 20px 20px;
}
.section-header {
font-weight: bold;
color: #333;
font-size: 18px;
}
.summary-title {
display: flex;
}
.cart-items {
margin-bottom: 60px;
}
.btn {
text-align: center;
vertical-align: middle;
padding: 0.67em 0.67em;
cursor: pointer;
}
.btn-primary {
color: white;
position: absolute;
background-color: #9100ff;
border: none;
border-radius: 4px;
font-weight: bold;
display: block;
font-size: 16px;
bottom: 20px;
width: 83%;
}
.btn-primary:hover {
background-color: #000099;
}
.main-line {
margin-right: 20px;
}
.cart-header {
font-weight: bold;
font-size: 15 px;
}
.cart-column {
display: flex;
align-items: center;
color: black;
margin-right: 1.5em;
padding-bottom: 10px;
margin-top: 10px;
}
.cart-column-title {
display: flex;
align-items: center;
color: lightgray;
margin-right: 1.5em;
padding-bottom: 10px;
margin-top: 10px;
}
.cart-row {
display: flex;
}
.cart-item {
width: 55%;
font-size: 14px;
text-transform: uppercase;
}
.cart-price {
display: flex;
justify-content: center;
width: 15%;
font-size: 14px;
text-transform: uppercase;
}
.cart-quantity {
display: flex;
justify-content: center;
width: 15%;
font-size: 14px;
text-transform: uppercase;
}
.cart-subtotal {
display: flex;
justify-content: center;
width: 15%;
font-size: 14px;
text-transform: uppercase;
}
.item-info {
display: flex;
flex-direction: column;
}
.cart-item-title {
color: #bc6cff;
margin-left: 10px;
font-size: 15px;
font-weight: bold;
}
.cart-item-description {
color: lightslategray;
margin-left: 10px;
font-size: 10px;
}
.cart-item-image {
width: 75px;
height: auto;
border-radius: 10px;
}
.cart-quantity-input {
height: 34px;
width: 34px;
border-radius: 5px;
border: 2px solid lightgray;
color: #333;
padding: 0;
text-align: center;
font-size: 1.2em;
margin-right: 15px;
margin-left: 15px;
}
.cart-row:last-child .cart-column {
border: none;
}
.cart-total {
text-align: end;
margin-top: 10px;
margin-right: 10px;
}
.cart-total-title {
font-size: 14px;
color: black;
margin-right: 20px;
text-transform: uppercase;
}
.cart-total-price {
color: #333;
font-size: 1.1em;
}
.numerical-values {
color: #bc6cff;
font-size: 34px;
}
.numerical-values:hover {
cursor: pointer;
}
.items-number {
display: flex;
}
.items-and-total {
display: flex;
flex-direction: row;
justify-content: space-between;
padding: 17px 0px 0px 17px;
}
.items-and-total-final {
display: flex;
flex-direction: row;
justify-content: space-between;
padding: 0px 0px 0px 0px;
}
.items-discount {
display: flex;
text-transform: uppercase;
padding-top: 15px;
}
.final-part {
margin-bottom: 70px;
bottom: 0px;
position: absolute;
width: 82%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>Shopping cart</title>
<meta name="description" content="This is the description" />
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="container">
<div class="first-section">
<h2 class="section-header">Shopping cart</h2>
<hr class="main-line" />
<div class="cart-row">
<span class="cart-item cart-header cart-column-title"
>Product details</span
>
<span class="cart-quantity cart-header cart-column-title"
>Quantity</span
>
<span class="cart-price cart-header cart-column-title">Price</span>
<span class="cart-subtotal cart-header cart-column-title">Total</span>
</div>
<div class="cart-items">
<div class="cart-row" id="0">
<div class="cart-item cart-column">
<img
class="cart-item-image"
src="Images/goku.png"
width="100"
height="100"
/>
<div class="item-info">
<span class="cart-item-title">Goku POP</span>
<span class="cart-item-description">Product code GOKU</span>
</div>
</div>
<div class="cart-quantity cart-column">
<div class="numerical-values minus">-</div>
<input
class="cart-quantity-input"
disabled
type="text"
value="0"
/>
<div class="numerical-values plus">+</div>
</div>
<span class="cart-price cart-column">5€</span>
<span class="cart-subtotal cart-column">0€</span>
</div>
<div class="cart-row" id="1">
<div class="cart-item cart-column">
<img
class="cart-item-image"
src="Images/naruto.png"
width="100"
height="100"
/>
<div class="item-info">
<span class="cart-item-title">Naruto POP</span>
<span class="cart-item-description">Product code NARUTO</span>
</div>
</div>
<div class="cart-quantity cart-column">
<div class="numerical-values minus">-</div>
<input
class="cart-quantity-input"
id="naruto"
disabled
type="text"
value="0"
/>
<div class="numerical-values plus">+</div>
</div>
<span class="cart-price cart-column">20€</span>
<span class="cart-subtotal cart-column">0€</span>
</div>
<div class="cart-row" id="2">
<div class="cart-item cart-column">
<img
class="cart-item-image"
src="Images/luffy.png"
width="100"
height="100"
/>
<div class="item-info">
<span class="cart-item-title">Luffy POP</span>
<span class="cart-item-description">Product code LUFFY</span>
</div>
</div>
<div class="cart-quantity cart-column">
<div class="numerical-values minus">-</div>
<input
class="cart-quantity-input"
id="luffy"
disabled
type="text"
value="0"
/>
<div class="numerical-values plus">+</div>
</div>
<span class="cart-price cart-column">7.5€</span>
<span class="cart-subtotal cart-column">0€</span>
</div>
</div>
</div>
<div class="second-section">
<div class="cart-total">
<h2 class="section-header summary-title">Order Summary</h2>
<hr />
<div class="items-and-total">
<span class="items-number">0 items</span>
<span class="cart-total-price">0€</span>
</div>
<hr />
<div class="final-part">
<hr />
<div class="items-and-total-final">
<div class="cart-total-title">Total cost</div>
<span class="cart-total-price">0€</span>
</div>
</div>
</div>
<button class="btn btn-primary" type="button">Checkout</button>
</div>
</div>
</body>
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"
></script>
<script src="store.js" async></script>
</html>