Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

264
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda