This is the code in JAVASCRIPT. BuyButton is every single button in the HTML document, and i tried to make it so that my switch uses the id of these buttons to determine which one of the buttons was clicked and perform the corresponding action. Im not sure i have used event.target.id correctly. The switch seems to recognize the first button with id "HW" but not any other.
const BuyButton = document.querySelector("button");
BuyButton.addEventListener("click", BuyButtonPressed);
function BuyButtonPressed(event){
switch (event.target.id)
{
case "HW":
if(energy >= 25)
{
energy -= 25;
HamsterWheels += 1;
}
break;
case "C":
if(energy >= 75)
{
energy -= 75;
Cyclists += 1;
}
break;
case "WG":
if(energy >= 175)
{
energy -= 175;
WindGenerators += 1;
}
break;
case "SP":
console.log(event.target.id);
if(energy >= 25)
{
energy -= 25;
SolarPanels += 1;
}
break;
case "NPP":
if(energy >= 25)
{
energy -= 25;
NuclearPowerPlants += 1;
}
break;
case "FPP":
console.log(event.target.id);
if(energy >= 12500)
{
energy -= 12500;
FusionPowerPlants += 1;
}
break;
}
updateStats();
}
This is the HTML code around the buttons.
<ul class="ShopBuyButtons">
<li><button id="HW">BUY for 25⚡</button></li>
<li><button id="C">BUY for 75⚡</button></li>
<li><button id="WG">BUY for 175⚡</button></li>
<li><button id="SP">BUY for 250⚡</button></li>
<li><button id="NPP">BUY for 2500⚡</button></li>
<li><button id="FPP">BUY for 12500⚡</button></li>
</ul>
Thankful for any help!