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
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 Respostas
Responde à pergunta

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 Relatório
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