Search code examples
javascriptjqueryhtmlcountdownjs.js

How do I insert <span> tags around the countdown numbers


I'm using this countdown code :

var clock = document.getElementById("timeleft"), tdy = new Date(1494979200000);
countdown.setLabels(
  '| <span class="time-label">second</span>| <span class="time-label">minute</span> | <span class="time-label">hour<span> | <span class="time-label">day</span> || <span class="time-label">month</span> ||||',
  '| <span class="time-label">seconds</span> | <span class="time-label">mins</span> | <span class="time-label">hours</span> | <span class="time-label">days</span> || <span class="time-label">months</span> ||||',
  '',
  '',
  'Out of Time','');
clock.innerHTML = countdown(new Date(tdy.getFullYear(), tdy.getMonth(), tdy.getDate(), tdy.getHours(), tdy.getMinutes(), tdy.getSeconds()) ).toString();
setInterval(function(){
  clock.innerHTML = countdown(new Date(tdy.getFullYear(), tdy.getMonth(), tdy.getDate(), tdy.getHours(), tdy.getMinutes(), tdy.getSeconds()) ).toString();
}, 1000);

How do I modify the above code to add span tags around the countdown numbers? thanks in advance.

Reference: http://explore.venasolutions.com/Q3-Weekly-Webinar-LP-2.html


Solution

  • If you want exactly what is displayed in the url you have given ,you would have to use the following code using Div tags,

    var arr=countdown(new Date(tdy.getFullYear(), tdy.getMonth(), tdy.getDate(), tdy.getHours(), tdy.getMinutes(), tdy.getSeconds()) ).toString();
            for(var i=0;i<arr.length;i++){
            clock.innerHTML= "<div> "+arr+" </div>";
            } 
            setInterval(function(){
                var arr=countdown(new Date(tdy.getFullYear(), tdy.getMonth(), tdy.getDate(), tdy.getHours(), tdy.getMinutes(), tdy.getSeconds()) ).toString();
                clock.innerHTML= "<div> "+arr+" </div>";
            }, 1000);
    

    In case you want the entire text within span tags as you had mentioned you would have to us the following code,

    var count=countdown(new Date(tdy.getFullYear(), tdy.getMonth(), tdy.getDate(), tdy.getHours(), tdy.getMinutes(), tdy.getSeconds()) ).toString();
            var arr=count.split(" ");
            clock.innerHTML="";
            for(var i=0;i<arr.length;i++){
                clock.innerHTML+= "<span> "+arr[i]+" </span>";
            } 
            setInterval(function(){
                var count = countdown(new Date(tdy.getFullYear(), tdy.getMonth(), tdy.getDate(), tdy.getHours(), tdy.getMinutes(), tdy.getSeconds()) ).toString();
                var arr=count.split(" ");
                clock.innerHTML="";
                for(var i=0;i<arr.length;i++){
                    clock.innerHTML+= "<span> "+arr[i]+" </span>";
                }
              }, 1000);