Very Simple Question, help appreciated, anyone?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Very Simple Question, help appreciated, anyone?

Post 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>
Last edited by scarface222 on Mon May 18, 2009 6:08 pm, edited 2 times in total.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Very Simple Question, help appreciated

Post 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.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Very Simple Question, help appreciated

Post 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.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Very Simple Question, help appreciated, anyone?

Post by scarface222 »

ANYONE AT ALL???!!!
myismymxd
Forum Newbie
Posts: 5
Joined: Wed May 20, 2009 10:41 am

Re: Very Simple Question, help appreciated, anyone?

Post by myismymxd »

Javascript shuold after the <div id="myAnchor"></div>
Is it ok??
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Very Simple Question, help appreciated, anyone?

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Very Simple Question, help appreciated, anyone?

Post by scarface222 »

Thanks alot all you guys. I put it at the bottom of the page and it works.
Post Reply