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

170
Visualizações
Click event listener working but not keydown?

Why does this work:

buttonGrid.addEventListener('click', e => {
  calculate(e);
});

let calculate = (e) => {
if (e.target.id === "back") {
displayArea.textContent = displayArea.textContent.substring(0, 
   displayArea.textContent.length - 1);
}
}

But not this...

buttonGrid.addEventListener('keydown', e => {
  calculate(e);
});

let calculate = (e) => {
if (e.keyCode === 8) {
displayArea.textContent = displayArea.textContent.substring(0, 
   displayArea.textContent.length - 1);
}
}

See lines 30-45 at: https://jsfiddle.net/s9ebLdvt/2/

If I log out the typeof e.keyCode I get number. Hence I use a number rather than a string.

I've also tried switching the === to == but get the same issue.

Weirdly if I place a console log in the if statement it fires it just doesn't compute the code?

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