Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda