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

195
Views
iterate through retrieved buttons by class name and click on them

I grab all elements by a specific class name like below:

var button = document.getElementsByClassName('submit')

I would like to iterate through what is retrieved and click on all buttons with that class name. I tried something like

for(var i in button){
  i.click()}

But that does not seem to work. How would I go about clicking each button that is retrieved?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There are lots of ways you can do it, but you can do it like this:

const btns = document.querySelectorAll('.anyButtonClass');

btns.forEach(function(btn){
  // when ever this code runs, it will automatically click all your buttons
  btn.click();
  console.log(btn.innerHTML);

  // btn.addEventListener('click', function(e){
    // console.log(btn.textContent);
  // })
})
<div class="btn-container">
  <button class="anyButtonClass">Click Me 1</button>
  <button class="anyButtonClass">Click Me 2</button>
  <button class="anyButtonClass">Click Me 3</button>
</div>

about 4 years ago · Juan Pablo Isaza Report
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!