Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

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

You are here: irt.org | FAQ | JavaScript | Form | 3.3 | Q750 [ previous next ]

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>

©2018 Martin Webb