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

177
Visualizações
JS change button value on click for multiple buttons

I have got 3 buttons and need to change their text when it's clicked using Javascript. It works fine for the first one but the rest of them doesn't get updated. I know one way of achieving this is by adding JS for different inputs and id. But I just want to write a single function which solves this issue.

<input id=-btn" type="button" value="ADD" />

<input id="btn" type="button" value="ADD" />

<input id="btn" type="button" value="ADD" />


document.getElementById("btn").addEventListener(
        "click",
        function (event) {
          if (event.target.value === "ADD") {
            event.target.value = "ADDED";
          } else {
            event.target.value = "ADD";
          }
        },
        false
      );
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

<input id=-btn" type="button" value="ADD" onclick="func(this)"/>

<input id="btn" type="button" value="ADD"  onclick="func(this)"/>

<input id="btn" type="button" value="ADD" onclick="func(this)"/>


<script>
        function func(event) {
          if (event.value === "ADD") {
            event.value = "ADDED";
          } else {
            event.value = "ADD";
          }
        }
      </script>
about 4 years ago · Juan Pablo Isaza Relatório

0

function changeValue(event) {
      if (event.target.value === "ADD") {
        event.target.value = "ADDED";
      } else {
        event.target.value = "ADD";
      }
    }
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Document</title>
</head>

<body>

  </head>

  <body>

    <input id="btn" onclick="changeValue(event)" type="button" value="ADD" />

    <input id="btn" onclick="changeValue(event)" type="button" value="ADD" />

    <input id="btn" onclick="changeValue(event)" type="button" value="ADD" />
  </body>



</body>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

</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