0

I have a basic time-clock script. I would like to have control over each digit. How would I add css to these variables? This is what I've tried.

window.onload = function startTime() {

          var today=new Date();
          var h=today.getHours();
              h="<span id='hours'>"+h+"</span>";
          var m=today.getMinutes();
          var s=today.getSeconds();
          // add a zero in front of numbers<10
          m=checkTime(m);
              m="<span id='minutes'>"+m+"</span>";
          s=checkTime(s);
              s="<span id='seconds'>"+s+"</span>";
          document.getElementById("txt").innerHTML=h+"<span class='time-colon'>:"+"</span>"+m+"<span class='time-colon'>:"+"</span>"+s;
          t=setTimeout(function(){startTime()},500);
          }

          function checkTime(i)
          {
          if (i<10)
            {
            i="0" + i;
            }
          return i;

         }

1 Answer 1

1

Use a span for each digit. h is a String, it is not an HTML element yet.

h="<span style='color:black;'>"+h+"</span>";

Note that CSS classes (in external stylesheets) are preferred to inline styles.

Sign up to request clarification or add additional context in comments.

1 Comment

I see. I was looking at it like h was an object. However, h is the result(string) of getHours, which then gets set into the html.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.