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

108
Vistas
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 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