Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda