-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
29 lines (22 loc) · 1.07 KB
/
script.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
const allFood = document.querySelectorAll('.food');
allFood.forEach(food => {
food.querySelector('.food-total').textContent = '$ 0.00';
});
allFood.forEach((food, index) => {
food.addEventListener('click', (event) => {
if(event.target.classList.contains('order-dec') || event.target.parentElement.classList.contains('order-dec')){
changeOrder(food, 'dec');
}
if(event.target.classList.contains('order-inc') || event.target.parentElement.classList.contains('order-inc')){
changeOrder(food, 'inc');
}
});
});
function changeOrder(food, changeType){
let foodQuan = parseInt(food.querySelector('.order-val').textContent);
let foodPrice = parseFloat(food.querySelector('.food-rate').textContent.replace(/[^\d.-]/g, '')); // replacing all non-digit characters
if(changeType === 'dec' && foodQuan > 0) foodQuan--;
if(changeType === 'inc') foodQuan++;
food.querySelector('.order-val').textContent = foodQuan;
food.querySelector('.food-total').textContent = `$ ${(foodQuan * foodPrice).toFixed(2)}`;
}