Skip to content
Advertisement

Trying to position JavaScript variables

I’m just learning javascript and I’m trying to center the theDate var by using the style tag but it stays to the far left anyone see where I’m going wrong?

function initArray() {

  this.length = initArray.arguments.length;
  for (var i = 0; i < this.length; i++)
    this[i + 1] = initArray.arguments[i];
}


var dayArray = new initArray("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

var monthArray = new initArray("januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december");

var nu = new Date();
var daily = dayArray[(nu.getDay() + 1)];
var day = nu.getDate();
var month = monthArray[(nu.getMonth() + 1)];
var jaar = nu.getYear();
var jaar4 = ((jaar < 1900) ? (jaar + 1900) : (jaar));

var theDate = (daily + ", " + day + " " + month + " " + jaar4);

document.write(theDate);
#theDate {
  text-align: center;
  padding: 5%;
}

Advertisement

Answer

your css is centering the element with the id theDate, so wrap that string in a with that id document.write('<div id="theDate">' + theDate + '</div>');

<style type="text/css">
        #theDate {
          text-align: center;
          padding: 5%;
        }
        </style>

      <script type="text/javascript" id="theDate">

      function initArray() {

        this.length = initArray.arguments.length;
        for (var i = 0; i < this.length; i++)
        this[i+1] = initArray.arguments[i];
      }
             

      var dayArray = new initArray("Sunday","Monday", "Tuesday","Wednesday","Thursday","Friday","Saturday");

      var monthArray = new initArray("januari","februari","maart","april","mei","juni","juli", "augustus","september","oktober","november","december");

      var nu = new Date();
      var daily = dayArray[(nu.getDay()+1)];
      var day = nu.getDate();
      var month = monthArray[(nu.getMonth()+1)];
      var jaar = nu.getYear();
      var jaar4 = ((jaar < 1900) ? (jaar + 1900) : (jaar));

      var theDate = (daily + ", " + day + " " + month + " " + jaar4);

      document.write('<div id="theDate">' + theDate + '</div>');

      </script>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement