View Single Post
Old 03-10-2012, 05:02 PM   PM User | #17
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
These examples ought to point you the right way:-

Code:
<form>
The Date <input type = "text" id = "date"><br>
Quantity 1<input type = "text" id = "qty1"><br>
</form>

<script type = "text/javascript">

window.onload = initForm;

function todayTxt() {
var Today = new Date();
return Today.getMonth()+1+"-"+Today.getDate()+"-"+Today.getFullYear();
}

function initForm() {
document.getElementById("date").value = todayTxt();  // address element by id
document.getElementById("qty1").focus();
}
</script>
Or

Code:
<form>
The Date <input type = "text" name= "date" id = "date"><br>
Quantity 1<input type = "text" name = "qty1" id = "qty1"><br>
</form>

<script type = "text/javascript">

window.onload = initForm;

function todayTxt() {
var Today = new Date();
return Today.getMonth()+1+"-"+Today.getDate()+"-"+Today.getFullYear();
}

function initForm() {
document.forms[0].date.value = todayTxt();   // address element by name
document.forms[0].qty1.focus();
}
</script>
Or, most correctly, assign the id "myform" to the form and then

Code:
function initForm() {
document.getElementById('myform').date.value = todayTxt(); // address form by id, form element by name
document.getElementById("myform").qty1.focus();
}

var document orders // the name of a variable may not contain a space.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 03-10-2012 at 05:39 PM..
Philip M is offline   Reply With Quote