If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Auto year select field in descending order

Hello, I am using this script to auto populate a select form field with the current year. Anyone know how to easily make the list show in descending order? I just can't figure it out. Thanks for your time!

/* This script and many more are available free online at
The JavaScript Source!! http://www.javascriptsource.com
Created by: Don Demrow | */
function autoYear() {
var time = new Date();
var year = time.getYear();

if (year < 1900) {
year = year + 1900;
}

var date = year - 25; /*change the '25' to the number of years in the past you want to show */
var future = year + 10; /*change the '10' to the number of years in the future you want to show */

First, I think getYear() is (or is being) deprecated; use getFullYear(), which will give you the full four digit year.

Code:

function autoYear() {
var time = new Date();
var year = time.getFullYear();
var date = year + 25; /*change the '25' to the number of years in the past you want to show */
var future = year - 10; /*change the '10' to the number of years in the future you want to show */
document.writeln ("<form><select><option value=\"\">Select Year");
do {
date--;
document.write ("<option value=\"" +date+"\">" +date+ "");
}
while (date > future)
document.write ("</select></form>");
}

IMHO, the above is kind of a hokey way of doing it. You're better off hard-coding the SELECT in HTML without options, giving it an ID, and then after the page is loaded use getElementById and new Option. Loop through that and use new Option to add the years.

Just my $0.02 worth.

|||||o . Q
_—__ "You live and you learn; or you don't live long." - Lazarus Long