Home > Back-end >  Trying to position JavaScript variables
Trying to position JavaScript variables

Time:11-14

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%;
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related