Contador Javascript de clicks

Question:

Suppose I have the following counter:

<button class="btn btn-primary" type="button">
    Curtir <span class="badge"> 4 </span>
</button>

I want to apply this code below:

function criaCounter(init) {
    var count = init || 0;
    return function() {
        count++;
        alert(count);
    }
}

$('#addCount').click(criaCounter(5));

so that it doesn't send an alert, but rather increase the number next to the like button, always adding infinitely every time someone clicks.

Answer:

You can take the number with Node.textContent and add +1 to each click. Always replacing textContent .

var contador = document.querySelector('.badge');

document.querySelector('button').addEventListener('click', function(){
  var numero = parseInt(contador.textContent) + 1;
  contador.textContent = numero;
});
<button class="btn btn-primary" type="button">
  Curtir <span class="badge"> 4 </span>
</button>
Scroll to Top