Question

I am trying to enter the current date when the page loads. I am able to store it to a variable, I am just having trouble entering that variable onto the page.

HTML:

<span id='test'>x</span>

Javascript:

window.onload = function() {
  var month = new Array("January", "February", "March", "April", "May", "June", "July",    "August", "September", "October", "November", "December");
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth();
  var currentMonth = month[mm];
  var yyyy = today.getFullYear();
  today = currentMonth + ' ' + dd + ', ' + yyyy;
  document.getElementById('test').innertext() = today;
}

JSFIDDLE

Was it helpful?

Solution

Change this:

document.getElementById('test').innertext() = today;

for this:

document.getElementById('test').innerHTML = today;

OTHER TIPS

Use this code, it works

window.onload = function() {
      var month = new Array("January", "February", "March", "April", "May", "June", "July",    "August", "September", "October", "November", "December");
      var today = new Date();
      var dd = today.getDate();
      var mm = today.getMonth();
      var currentMonth = month[mm];
      var yyyy = today.getFullYear();
      today = currentMonth + ' ' + dd + ', ' + yyyy;
      document.getElementById('test').innerHTML = today;
    }

You have a problem at the following line of code because you treat the innerText property as if it was a function and you don't camel-case it (JavaScript is a case-sensitive language):

 document.getElementById('test').innertext() = today;

So change it to:

document.getElementById('test').innerText = today;

Here is the working example: http://jsfiddle.net/ynevet/cNCf4/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top