How to load a javascript function in html (span tag)

Stoyan Denkov picture Stoyan Denkov · Nov 26, 2013 · Viewed 10.9k times · Source

Here I have an HTML Element:

<h3>Current Date: <span id="spanDate"></span></h3>

How am I able to (on)load the function below between the span tag

function aDate()
{
var d=new Date();
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
document.write(weekday[d.getDay()] + " ");
document.write(d.getDate() + ". ");
document.write(monthname[d.getMonth()] + " ");
document.write(d.getFullYear());
}

Thank you!

Answer

sbeliv01 picture sbeliv01 · Nov 26, 2013

Try this:

window.onload = function() {
    var d = new Date();
    var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var monthname = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

    // Set your output to a variable
    var output = weekday[d.getDay()] + " " + d.getDate() + ". " + monthname[d.getMonth()] + " " + d.getFullYear();

    // Target the ID of the span and update the HTML
    document.getElementById('spanDate').innerHTML = output;
};

Fiddle: http://jsfiddle.net/QBsWy/8/