Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
My switch with event.target.id does not work, the first case is the only one that works. JAVASCRIPT

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!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!