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

183
Visualizações
How to get the Value in the labels for the 2 independent buttons?

I have two buttons with a single label like the below. The two buttons were independent.

<label for="buttons" class ="val">0</label>
      <input class="btn btn-primary button1" type="button" value="button1" onclick="changeLabel()">
      <input class="btn btn-primary button2" type="button" value="button2">

When I click the button1 1sttime, the Value in the label should be 1, for the 2nd time, the value in the label should be 2 and for the 3rd time, the value in the label should be 3. When I click the button1 for the 4th time, the value should again go back to 0. Like 0,1,2,3,0,1.....

The same should be the case for the button2 too. But, If i press the button1 2times. Then the Value in the label should be 2.At the same time, if I press the button2 for 3times the value in the label should change to 3 immediately.

Both the buttons, should be independent. The Values should be different for each buttons when they were clicked.

(".button1").click(function(){
     var value = $('.val').html(parseInt($('.val').html())+1);
});

$(".button2").click(function(){
     $('.val').html(parseInt($('.val').html())+1);
});

I couldn't do so. The value should return to 0 after clicking the buttons 3 times. here is my codepen, https://codepen.io/Davi9/pen/LYLqWKx

could anyone please help?

Many thanks.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you want to reset the counter to 0 when it reach 3, then try this:

$(".button1").click(function() {
  var v = +$(".val").html();
  $(".val").html(v > 2 ? 0 : v + 1);
});

Demo

$(".button1").click(function() {
  var v = +$(".val").html();
  $(".val").html(v > 2 ? 0 : v + 1);
});

$(".button2").click(function() {
  var v = +$(".val").html();
  $(".val").html(v > 2 ? 0 : v + 1);
});
body {
  font-family: system-ui;
  background: #f06d06;
  color: white;
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<body>
  <div class="container">
    <div class="row ">
      <div class="col-md-6">
        <br />
        <label for="buttons" class="val">0</label>
        <br />
        <input class="btn btn-primary button1" type="button" value="button1">
        <input class="btn btn-primary button2" type="button" value="button2">

      </div>
    </div>
  </div>
</body>

about 4 years ago · Juan Pablo Isaza Relatório

0

Check the value of the label, if its greater than 3, set it to zero, somthing like

(".button1").click(function(){
   incr();
});

$(".button2").click(function(){
   incr();
});

function incr() {
  var val = parseInt($('.val').html());
  val=val+1;
  if(val > 3)
    val = 0;
  $('.val').html(val);
}
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