Reputation: 1233
Hey guys i have some code here that is supposed to create HTML elements on the page that will display today date next to ID='today' How do use a textnode to write out to the ID? Thanks for any help! Here is my Full code:
HTML:
<body>
<h1>ToDo List - Date: <span id='today'> </span></h1>
<div id="todolist">
<p>
<input type="button" id="additem" value="Add Item">
</p>
</div>
<hr>
<div>
<p>
<input type="button" id="sortitems" value="Sort and Display Items">
</p>
<p id="displayitems">
</p>
</div>
</body>
Javascript:
var $ = function (id){
return document.getElementById(id);
}
var TodaysDate = function () {
var myDate = new Date();
var myMonth = myDate.getMonth() + 1;
var myDay = myDate.getDate();
var myYear = myDate.getFullYear();
var myResponse = myMonth + "/" + myDay + "/" + myYear;
var myPara = document.createElement("p");
var myP = document.getElementById('today');
myP.appendChild(myPara);
var myText = document.createTextNode(myResponse);
myPara.appendChild(myText);
}
window.onload = function ()
{
$("today").onload = TodaysDate;
$("sortitems").onclick = sortItem;
$("additems").onclick = addItem;
}
Upvotes: 2
Views: 120
Reputation: 178285
Span onload? Onload is not triggered for a span. Other object do have a load event for example: windows/frames, body, link, script and images
Change
$("today").onload = TodaysDate;
To
TodaysDate();
and
var myPara = document.createElement("p");
var myP = document.getElementById('today');
myP.appendChild(myPara);
var myText = document.createTextNode(myResponse);
myPara.appendChild(myText);
To
var myPara = document.createElement("p");
var myText = document.createTextNode(myResponse);
myPara.appendChild(myText);
var myP = $('today');
myP.appendChild(myPara);
Upvotes: 2
Reputation: 2329
Try replacing this line:
$("today").onload = TodaysDate;
with this line:
TodaysDate();
because the SPAN
tag does not support the onload
event (see this answer).
Try it out here.
Upvotes: 1