Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto Date Selection Script Help 1

Status
Not open for further replies.

panini

MIS
Jun 1, 2001
136
GB
hi i currently use this for a form that has individual day/month/year drop downs:

function fromDate()
{
today = new Date();

d = today.getDate();
m = today.getMonth();
y = today.getFullYear();

document.SelForm.ddd.selectedIndex = d+0;
document.SelForm.ddm.selectedIndex = m+0;
document.SelForm.ddy.selectedIndex = y-2005;

}


but i have a form that use the following format:

<option value="1/2006">Jan 06</option>
<option value="2/2006">Feb 06</option>
and so on...................

how can i ammend that script that that Nov 06 becomes the default month throughout november?????
 
You could try setting the value of the select element... Something like this:

Code:
document.forms['formName'].elements['selElName'].value = d + '/' + y;

Failing that, you'll have to loop around the options, testing each to determine if the value is the one you desire.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
no joy - ive tried this but just defaults to the top one

i should mention that you probably meant to put a M rather than a D as 1/2006 means jan 2006, not the 1st day of 2006

but either way i couldnt get it working :|

<script>
today = new Date();

d = today.getDate();
m = today.getMonth();
y = today.getFullYear();

document.forms['ahQuote'].elements['arrivalMonthYear'].value = m + '/' + y;
</script>
 
Change this:

Code:
m = today.getMonth();

to this:

Code:
m = today.getMonth() + 1;

the getMonth() method returns months indexed starting at 0: 0 - 11

You also might wanna stick in an alert just to see what your program is doing:
Code:
<script [!]type="text/javascript"[/!]>
[!]var[/!] today = new Date();

[!]var[/!] d = today.getDate();
[!]var[/!] m = today.getMonth() [!]+ 1[/!];
[!]var[/!] y = today.getFullYear();

[!]var a = m + '/' + y;

alert(a);[/!]

document.forms['ahQuote'].elements['arrivalMonthYear'].value = [!]a[/!];
</script>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
alert worked fine, 11/2006 but didnt touch the date in the form

the page errored saying

document.forms['ahQuote'].elements is null or not an object
must be close!!!
 
Take a look at the view-source of your page. Search for a form named ahQuote (make sure there is only one), then make sure that there is an element inside that form that is named arrivalMonthYear (make sure there is also only one).

The error blah is null or not an object means that the object you are trying to reference does not exist.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
dam we r so close, both elements r there

does it matter if the form has the same id and name, would it interefer??????? well just tried it and didnt make a difference having either/or

what about positioning of the script - is this key?
 
does it matter if the form has the same id and name

Nope

what about positioning of the script - is this key?

It shouldn't matter

Can you post a link to your site, or possibly a view-source of your page?

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
was a bit of craziness as the original script i came up with on a different site ran after the form had loaded, but had never considered that, works great now though so many thanks for all involved :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top