Page 1 of 1

Very Simple Question, help appreciated, anyone?

Posted: Mon May 18, 2009 3:57 pm
by scarface222
Hey guys I have the following date script which prints the current date. Is there any way to use document.getElementById to make this script print on a selected div? I tried deleting document write and writing document.getElementById("date")= and that did not work.

Code: Select all

<script language="Javascript">
<!--
 
 var dayName = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
 
 var monName = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
 
 var now = new Date
 
 document.write("Today is " + dayName[now.getDay()] + ", " + monName[now.getMonth()] + " "+now.getDate() +" "+ now.getYear()+".")
 
//-->
</script>

Re: Very Simple Question, help appreciated

Posted: Mon May 18, 2009 4:04 pm
by Zoxive
You could use innerHTML.

Code: Select all

var today = "Today is " + dayName[now.getDay()] + ", " + monName[now.getMonth()] + " "+now.getDate() +" "+ now.getYear()+".";
 
document.getElementById('myAnchor').innerHTML = today;
As the name suggests it is the direct html that is in the element, so you can add html as you please.

Re: Very Simple Question, help appreciated

Posted: Mon May 18, 2009 4:51 pm
by scarface222
Hey thanks for the reply man but when I used your method and used <div id="myAnchor"></div>, I did not get anything displayed.

Re: Very Simple Question, help appreciated, anyone?

Posted: Tue May 19, 2009 7:46 pm
by scarface222
ANYONE AT ALL???!!!

Re: Very Simple Question, help appreciated, anyone?

Posted: Wed May 20, 2009 10:53 am
by myismymxd
Javascript shuold after the <div id="myAnchor"></div>
Is it ok??

Re: Very Simple Question, help appreciated, anyone?

Posted: Wed May 20, 2009 12:23 pm
by pickle
The javascript should be included on the page after the DOM has been fully parsed. In other words, put it at the bottom of the page.

Re: Very Simple Question, help appreciated, anyone?

Posted: Wed May 20, 2009 10:18 pm
by scarface222
Thanks alot all you guys. I put it at the bottom of the page and it works.