PDA

View Full Version : getDate to be inside box


tiingshii
08-25-2009, 10:22 AM
hi ppl~

im trying to have the date inside the input text but i cnt get it to work... anyone can help? or is there any other way to have the date inside a box?

<html>
<body>

<input type="text" name="ShoesSize2" size="35">
<script type="text/javascript">
var d = new Date();
var e=new Date();
var f = new Date()

var month=new Array(12);
month[0]="1";
month[1]="2";
month[2]="3";
month[3]="4";
month[4]="5";
month[5]="6";
month[6]="7";
month[7]="8";
month[8]="9";
month[9]="10";
month[10]="11";
month[11]="12";


document.write(d.getDate() + "/");
document.write(month[e.getMonth()]+ "/");
document.write(f.getYear());

</script>

</body>
</html>

tadster
08-26-2009, 12:23 AM
<div id="toBeStrict"><input type="text" name="ShoesSize2" size="35">
</div>

<script type="text/javascript">
var d = new Date();

var month=new Array(12);
month[0]="1";
month[1]="2";
month[2]="3";
month[3]="4";
month[4]="5";
month[5]="6";
month[6]="7";
month[7]="8";
month[8]="9";
month[9]="10";
month[10]="11";
month[11]="12";

document.getElementById('toBeStrict').firstChild.v alue =
d.getDate() + "/"+month[d.getMonth()]+ "/"+d.getFullYear().toString().substring(2);

</script>



you can also give the input an id, but that's not strict.
Also there is no need for an e or f, and please use getFullYear not getYear (y2k bug).
hope this helps

Edit: important note: The input must be the very first thing after the opening of the div;
a new line would also count as a first child.

tiingshii
08-26-2009, 03:01 AM
wow it worked!!! thank you so much tadster!

jasonJ
08-26-2009, 07:13 PM
you can also give the input an id, but that's not strict.
What exactly is not strict in giving an id attribute to an input field?

tadster
08-29-2009, 05:04 PM
woops, you can give it an id and still validate as strict, but that can be confusing, so i always just use a name and the div around it to reference the input field.

if your going to give it an id, it must have a name also, which should be the same value as the id.