1

I am trying to replace the following's code of simple countdown timer getElementById('countdown') with getElementsByClassName('timer') instead but the code gets broken. Any idea?

var seconds = 60;
function secondPassed() {
  var minutes = Math.round((seconds - 30)/60);
  var remainingSeconds = seconds % 60;
  if (remainingSeconds < 10) {
    remainingSeconds = "0" + remainingSeconds; 
  }
  document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
  if (seconds == 0) {
    clearInterval(countdownTimer);
    document.getElementById('countdown').innerHTML = "Buzz Buzz";
  } else {
    seconds--;
  }
}

var countdownTimer = setInterval('secondPassed()', 1000);
<span id="countdown" class="timer"></span>
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
mediaroot
  • 435
  • 8
  • 17

1 Answers1

1

In this case, you can use document.getElementsByClassName("timer")[0] instead of getElementById('countdown').

bigonez
  • 111
  • 2