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

89
Vistas
problem with changing images and svg path using event listeners on click

i am working on an assignment where clicking on a button will change the facial expression and the bg image of the button.

so when i click on the button, my text is able to change from happy - sad - happy etc. however, the code does not work the same for my image, where the image is not changing at all onclick, likewise for the changing of the mouth path, the click event listener only works on the first click(changing sad face to happy face). it does not work for subsequent clicks. may I know if anyone has any idea where i went wrong?

This is the javascript code i have now:

let buttonState = "Sad";

let toggeleButton = document.getElementById("button");
toggeleButton.addEventListener("click", function(ev){

console.log("toggle button is working");
if(ev.target.textContent === 'Sad'){

    buttonState = "Happy";
    //changing button text and image
   ev.target.textContent = "Happy";
   ev.target.background ="https://cdn.pixabay.com/photo/2016/07/10/10/54/marguerite-1507550_960_720.jpg";
   
   //step 4
   mouth.attr({
        "path" : (`M 200,150 Q 300,250 450,150`)});

    } else {

        buttonState = "Sad";
        //changing button text and image
       ev.target.textContent = "Sad";
       ev.target.background = "https://cdn.pixabay.com/photo/2014/09/21/14/39/surface-455124_960_720.jpg";

       //step 4
       mouth.attr({
            "path" : (`M 200,150 Q 300,250 450,150`)});
     }

});

For my HTML code:

<button id="button" class="button">Sad</button>
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