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

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