New coder here. I am creating a pizza menu app that adds up the values assigned to the topping buttons and displays them in the Total at the bottom of the list. But I am struggling to get the code to display the cost of the toppings as they are added.
Below is what I attempted:
let whiteWheat = document.getElementById("white-wheat");
let multiGrain = document.getElementById("multi-grain");
let caulifl = document.getElementById("cauliflower");
let thinCrust = document.getElementById("thin-crust");
let deepDish = document.getElementById("deep-dish");
let totalField = document.getElementById("total");
totalField = 0;
whiteWheat.addEventListener(click, function() {
var totalCost = totalField + 10;
var finalOrder = document.createElement("li");
finalOrder.innerText = totalCost.value;
totalField.appendChild(finalOrder);
});
<h1>PIZZA SUPREME</h1>
<h2>Select a crust and toppings to build your own pizza!</h2>
<h3>Crust</h3>
<h4>$10 per crust.</h4>
<ul><button id="white-wheat">white wheat</button></ul>
<ul><button id="multi-grain">multigrain</button></ul>
<ul><button id="cauliflower">Cauliflower</button></ul>
<ul><button id="thin-crust">Thin Crust</button></ul>
<ul><button id="deep-dish">Deep Dish</button></ul>
<h2>Total:</h2>
<div id="total">
</div>
3
Answers
You are so close! Firstly, there is no such word as click, what you wanted to do is
'click'
. Secondly, you were usingtotalCost
(the element reference) also as a number. What you wanted to do is two variables.Tried keeping it simple:
EDIT: I’m not sure why are you creating an
li
element. Perhaps you wanted to set theinnerText
oftotalField
only?A nicer approach might be to create a
x-price
attribute on the buttons and put them in a<div>
. This way, you can use a single event listener and event propagation to add the values of thex-price
attribute. Like this:Alternatively, you can forgo the
x-price
attribute if they all cost the same: