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

94
Views
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
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!