Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q750 How can I pass the current date to a select form fieldmMenu and have it selected when the page is loaded?

irt.org | Knowledge Base | JavaScript | Form 3.3 | Q750 [ previous next ]

Q750 How can I pass the current date to a select form fieldmMenu and have it selected when the page is loaded?

Try:

<SCRIPT LANGUAGE="JavaScript"><!--
function padout(number) { return (number < 10) ? '0' + number : number; }

var today = new Date()

var ftoday = padout(today.getDate()) + '/' + padout((today.getMonth() + 1)) + '/' + today.getYear()

var output = '<FORM><SELECT>';

for (var i=-7; i<7; i++) {
    aday = new Date(today.getYear(),today.getMonth(),today.getDate() + i)
    faday = padout(aday.getDate()) + '/' + padout((aday.getMonth() + 1)) + '/' + aday.getYear()

    output += '<OPTION ';

    if (faday==ftoday)
        output += ' SELECTED';

    output += '>' + faday;
}

output += '<\/SELECT><\/FORM>';

document.write(output);
//--></SCRIPT>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.