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

171
Views
how to register click on a button without onclick event?

I have like 30 or so buttons in my react app. and i don't want to add event-Listener to each and every button. but every button has a specific job to do. So is there any way i can achieve this.

something like this(its depreciated now):

$("button").click(() => {
    fetch("https://reqres.in/api/users/" + this.value)
      .then((res) => res.json())
      .then((result) => setUser(result));
  });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I added the same class names to DOM elements which you want to add the event listeners and those elements by using getElementsByClassNames method. Then, added the event listener to them.

let count = 0;
function onClick() {
  document.getElementById("count").innerText = count++;
}
const buttons = document.getElementsByClassName("btn-multiple");
[...buttons].forEach(btn => btn.addEventListener("click", onClick));
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Simple scroll example</title>
    <style>
    </style>
  </head>
  <body>
    <section id="clear-section" class="clear-section">
      <input type="button" value="V1" class="btn-multiple" />
      <input type="button" value="V2" class="btn-multiple" />
      <input type="button" value="V3" class="btn-multiple" />
      <input type="button" value="V4" class="btn-multiple" />
      <input type="button" value="V5" class="btn-multiple" />
      <input type="button" value="V6" class="btn-multiple" />
      <p id="count"></p>
    </section>
  </body>
</html>

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!