How can i disable my button after i clicked it 10 times and it will say "Countdown Completed" in javascript

var count = 10;
function countdown() {
  count--;
  $("#count").text(count);
  console.log(count);
  countComplete();
}

function countComplete() {
  if (count <= 0) {
    document.getElementById("count").innerHTML="done";
  }
}

1 answer

  • answered 2018-10-11 19:32 Eugene Tsakh

    Basically you just need to decrease your counter on every click and if it becomes 0 then change button text and disable it.

    let clicksLeft = 10;
    const $count = $('#count').text(clicksLeft);
    
    $('button').on('click', function() {
      --clicksLeft;
      $count.text(clicksLeft);
    
      if (!clicksLeft) {
        this.innerText = 'Countdown Completed';
        this.disabled = true;
      }
    });