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

265
Visualizações
Count each value on button

I try to count each value in each button

This is the example

The PHP Code

<?php for($i = 0; $i < 5; $i++){?>
<a class="btn btn-primary btn-count" data-id="<?php echo($i)?>" id="click[<?php echo($i)?>]"> 
Click Me <h7 id="count[<?php echo($i)?>]">(0)</h7>
</a>
<?php }?>

Jquery

$(document).ready(function() {
  var i = 0;
  $(".btn-count").click(function(){
     i++;
     var id = $(this).data('id');
     $("#count["+id+"]").text(i);
  });
}

But it didn't work at all. Anyone can tell me where this code goes wrong?

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

0

You don't need to use the id's for this, you can use .find() to find the child based on your html.

Then get the "count" value from your element and add 1 to it.

$(document).ready(function() {
  $(".btn-count").click(function() {
    $(this).find("h6").text(function(i, n) {
      var count = +n.match(/\(([^)]+)\)/)[1];
      return `(${count+1})`
    });
  });
})

Demo

$(document).ready(function() {
  $(".btn-count").click(function() {
    $(this).find("h6").text(function(i, n) {
      var count = +n.match(/\(([^)]+)\)/)[1];
      return `(${count+1})`
    });
  });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="btn btn-primary btn-count" data-id="<?php echo($i)?>" id="click[<?php echo($i)?>]"> 
Click Me <h6 id="count[<?php echo($i)?>]">(0)</h6>
</a>


<a class="btn btn-primary btn-count" data-id="<?php echo($i)?>" id="click[<?php echo($i)?>]"> 
Click Me <h6 id="count[<?php echo($i)?>]">(0)</h6>
</a>

Problem with having a global counter as you have in var i = 0; it will no represent the button you have click, but all of them.

about 4 years ago · Juan Pablo Isaza Relatório

0

your code can be simply modified to this

<button class="btn btn-primary btn-count" data-id="1" id="click1"> 
Click Me <h7 id="count1">(0)</h7>
</button>

<button class="btn btn-primary btn-count" data-id="2" id="click2"> 
Click Me <h7 id="count2">(0)</h7>
</button>
$(document).ready(function() {

  $(".btn-count").click(function(){ 
     var id = $(this).data('id');
    var value = $(this).data('value');
    value++
    $(this).data('value', value);
    console.log(id)
    console.log( $("#count"+id))
     $("#count"+id).text(value);
  });
})

Here is also a demo for the code

https://codepen.io/BubuKiki/pen/YzegWMg

The problem is with the special characters, it does not seem to find them. Hope this helps.

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