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

112
Views
how to add the number of global likes updated according to the user click

here is my problem: When I click on a like, it is incremented by 1. This works for all photos that can be liked. I would like to add the total number of likes based on user clicks. I already have my ternary function to add or remove the like but I can't seem to sum the total likes and display it in the console. I can't find the solution because inside my event I don't have access to the total of likable photos (just one of each) and if I leave my event I don't have access to my incrementation function ... Can you help me ? Thanks in advance !

PS : I specify that at the base there is already a number of likes displayed which depends on each photo (data coming from a json file), the user adds his like or removes it by clicking on it.

 // incrementation likes 
    const likeContainers = document.querySelectorAll(".like-container");

    likeContainers.forEach((likeContainer) => {
        let tmpLike = Number(likeContainer.childNodes[0].innerHTML);
        
        
        likeContainer.addEventListener("click", (e) => {
            const g = likeContainer.childNodes[0].innerHTML;
            const like = Number(g);
            const incrementLikePhoto = 
            like === tmpLike ? like + 1 : tmpLike;
            likeContainer.childNodes[0].innerHTML = incrementLikePhoto;  
        });  
    });
    
    //likes sum 
    const reducer = (acc, curr) => acc + curr;
    let sum = findTmpLikes.reduce(reducer);
    const totalPriceContainer = document.createElement("div");
    totalPriceContainer.setAttribute("classe", "total-price__container");
    totalPriceContainer.append(sum)
    stickyCard.append(totalPriceContainer);
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!