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

220
Visualizações
Using an EventListener on the Element that calls a counter function - Javascript

Goal: Write a function that is called by the eventListener click event of an IMG. The function will use a conditional to see on which ElementID called the function, then initialize the on accumulator to that value, then add 1 and change the innerHTML to show that new value. Key goal is to make this occur with out have 3 similar identical functions

Code Thus Far

let likeCount = !NaN;

var classname = document.getElementsByClassName("like-heart");

function likeCounter() {
/*Note the elementID is very similar to the ELementID of the accumulator element - but not the same.*/
       if (this.getElementById === "ft-recipe-like") {
        likeCount = Number(document.getElementById('featured-likes').innerText);
        likeCount += 1;
        document.getElementById('featured-likes').innerText = likeCount;
    } else if (this.getElementById === "macaroon-like") {
        likeCount = Number(document.getElementById('macaroon-likes').innerText);
        likeCount += 1;
        document.getElementById('macaroon-likes').innerText = likeCount;
    } else if (this.getElementById === 'brulee-like') {
        likeCount = Number(document.getElementById('brulee-likes').innerText);
        likeCount += 1;
        document.getElementById('brulee-likes').innerText = likeCount;
    }    
for (var i = 0; i < classname.length; i++) {
        classname[i].addEventListener('click', likeCounter, false);
    }
}
}
    }
    for (var i = 0; i < classname.length; i++) {
        classname[i].addEventListener('click', likeCounter, false);
    }
}
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can have a map of acc id to Dom node id, and write a generic function to increment the likeCount.

let likeCount = !NaN;

var classname = document.getElementsByClassName("like-heart");

const accIdToDomIdMap = {
  'ft-recipe-like': 'featured-likes',
  'macaroon-like': 'macaroon-likes',
  'brulee-likes': 'brulee-likes'
}

function likeCounter() {
/*Note the elementID is very similar to the ELementID of the accumulator element - but not the same.*/
   if(accIdToDomIdMap.hasOwnProperty(this. getElementById)) {
      const domId = document.getElementById(accIdToDomIdMap[this.getElementById]);
      likeCount = (+domId.innerText || 0) + 1;
      domId.innerText = likeCount;
   }
}
function newFunction() {
    for (var i = 0; i < classname.length; i++) {
        classname[i].addEventListener('click', likeCounter, false);
    }
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
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