Search code examples
javascripthtmlcounter

JS countdown multiple times on page (won't display with getElementsByClassName)


I took some JS code from w3 to create a countdown. (https://www.w3schools.com/howto/howto_js_countdown.asp)

Because I want to display the countdown multiple times on one page I changed the getElementById("demo") to > getElementsByClassName("demo")

Unfortunately, this doesn't work. Nothing shows up. Why is that and how can I display the same counter multiple times? I tried some things but nothing worked out. This is my code:

html

<p class="demo"></p>

js

// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2022 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get today's date and time
  var now = new Date().getTime();
    
  // Find the distance between now and the count down date
  var distance = countDownDate - now;
    
  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    
  // Output the result in an element with id="demo"
  document.getElementsByClassName("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";
    
  // If the count down is over, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementsByClassName("demo").innerHTML = "EXPIRED";
  }
}, 1000);

Solution

  • As @ShanieMoonlight mentioned you need to iterate over the HTMLCollection. You can easily do it with minimal adjustments. E.g. when you use the spread-operator the forEach-function will be available.

    // Set the date we're counting down to
    var countDownDate = new Date("Jan 5, 2022 15:37:25").getTime();
    
    // Update the count down every 1 second
    var x = setInterval(function() {
    
      // Get today's date and time
      var now = new Date().getTime();
        
      // Find the distance between now and the count down date
      var distance = countDownDate - now;
        
      // Time calculations for days, hours, minutes and seconds
      var days = Math.floor(distance / (1000 * 60 * 60 * 24));
      var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      var seconds = Math.floor((distance % (1000 * 60)) / 1000);
        
      // Output the result in an element with id="demo"
      [...document.getElementsByClassName("demo")].forEach(e => e.innerHTML = days + "d " + hours + "h "
      + minutes + "m " + seconds + "s ");
        
      // If the count down is over, write some text 
      if (distance < 0) {
        clearInterval(x);
        [...document.getElementsByClassName("demo")].forEach(e=>e.innerHTML = "EXPIRED");
      }
    }, 1000);
    <p class="demo"></p>
    
    <p class="demo"></p>
    
    <p class="demo"></p>