Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!