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

91
Visualizações
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 à 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