Loading Inital Value for date using javascript help

andrew.daniels

New Member
Okay guys. Need some help here. I want to load the current date into the inital value of a text field using javascript any help here? where have I gone wrong.

<tr>
<td><label>Preferred Commencement Date:</label>&nbsp;

<script type="text/javascript">var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var date = (month + "/" + day + "/" + year)
</script>

</td>
<td><label>

<input name="date" type="text" id="date" onLoad="document.getElementById(date)" />
</label></td>
</tr>

First time using javascript so I'm pretty nooby - normally I'd just write Today and leave it at that, I've used w3schools and a few other tutorials to get to this stage.
 

leroy30

New Member
Hi Andrew.

document.getElementById(ID) gets en HTML element by its ID. For example a DIV with ID="blah" can be retrieved or accessed via javascript like this:

var myDiv = document.getElementById("blah");

Then we could change properties such as the style by doing this:

myDiv.style.width = "100px";

Additionally, an input element does not have an "onLoad" event. On the other case a "body" element does.

Ok. Now that we have cleared that up what you really want to do is this.

<input name="date" type="text" id="date" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
<script type="text/javascript"> var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var date = (month + "/" + day + "/" + year)
document.getElementById("date").value = date.toString();
</script>
 

andrew.daniels

New Member
You sir are a wizard.

Thankyou 100 times over for your assistance.

I dropped out of university last year and I've been hating myself since then. I start back at TAFE in a month so I can bridge back into uni. I see how important it is to learn web design properly now. Thanks so much. I'll drop a link to the completed website to you when its done.

THANKYOU!
 

CaldwellYSR

Member
Hi Andrew.

document.getElementById(ID) gets en HTML element by its ID. For example a DIV with ID="blah" can be retrieved or accessed via javascript like this:

var myDiv = document.getElementById("blah");

Then we could change properties such as the style by doing this:

myDiv.style.width = "100px";

Additionally, an input element does not have an "onLoad" event. On the other case a "body" element does.

Ok. Now that we have cleared that up what you really want to do is this.

<input name="date" type="text" id="date" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
<script type="text/javascript"> var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var date = (month + "/" + day + "/" + year)
document.getElementById("date").value = date.toString();
</script>

Wouldn't it be better to make the date a placeholder rather than the value?
 

leroy30

New Member
Andrew - No probs!
Caldwell - Are you refering to the HTML5 placeholder? I'd say not at this stage unless you can be bothered handling it properly when the browser doesn't support it (such as still widely used versions of IE).

Besides best not to confuse a newbie ;) There are more solid ways to achieve this but for what they were wanting it does the trick.
 

CaldwellYSR

Member
Andrew - No probs!
Caldwell - Are you refering to the HTML5 placeholder? I'd say not at this stage unless you can be bothered handling it properly when the browser doesn't support it (such as still widely used versions of IE).

Besides best not to confuse a newbie ;) There are more solid ways to achieve this but for what they were wanting it does the trick.

Didn't realize that was html5 :p good to know thanks
 
Top