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

133
Visualizações
Invalid writing of the forEach method

I have a question about foreach and subsequent capture of clicks.

When I use foreach like this, it works.

 let buttons = document.getElementsByClassName('button');
    
    for (let oneButton of buttons) {
        oneButton.addEventListener('click',function(){
            console.log('ahoj');
        })
    }

If I use it, but this is how it will write me a error: Uncaught TypeError: buttons.forEach is not a function

buttons.forEach(function(oneButton){
        oneButton.addEventListener('click',function(){
            console.log('Zdarec')
        })
    })

Can anyone explain to me why this tells me that forEach is not a function? It is clear to me that I can use the first option and move on, but I would like to know why the second option does not work.

I tried searching the internet, but I didn't find anything.

I will be happy for any answer.

Thanks :-)

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