So basically I am making an online restaurant website where you can order food. I am going to make cards for each food item listed. Right now, I am making buttons that add and subtract the number of each item the customer wants to purchase.
var counter = document.querySelector(".count");
var currentNumber = 0;
var classicBurger = 5.18;
var bargeBurger = 6.13;
function addOne(price) {
currentNumber += 1;
counter.innerHTML = currentNumber;
document.querySelector(".total-price").innerHTML =
"$" + (price * currentNumber).toFixed(2);
}
function subtractOne(price) {
if (currentNumber > 0) {
currentNumber += -1;
counter.innerHTML = currentNumber;
document.querySelector(".total-price").innerHTML =
"$" + (price * currentNumber).toFixed(2);
} else {
counter.innerHTML = 0;
}
}
<html>
<div class="food-item">
<h3 class="item-title">Classic Burger</h3>
<p class="item-description">
Classic beef patty with lettuce, tomato, and cheese.
</p>
<div class="add-subtract-items">
<button class="subtract-item" onclick="subtractOne(classicBurger)">
-
</button>
<span class="count">0</span>
<button class="add-item" onclick="addOne(classicBurger)">+</button>
</div>
<p class="total-price">$0.00</p>
</div>
<div class="food-item">
<h3 class="item-title">Barge Burger</h3>
<p class="item-description">
Classic beef patty with lettuce, tomato, and cheese.
</p>
<div class="add-subtract-items">
<button class="subtract-item" onclick="subtractOne(bargeBurger)">
-
</button>
<span class="count">0</span>
<button class="add-item" onclick="addOne(bargeBurger)">+</button>
</div>
<p class="total-price">$0.00</p>
</div>
<html/>How do I get the second button to effect the correct HTML rather than the total of the first item?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/GLMlfNh
Comments
Post a Comment