Re: JavaScript - How to add preset values to form inputfor select box -
11-10-2005
, 01:15 PM
Jonathan,
Probably several ways to do this, but here's one. Note that in this example
the <form> is named 'testform' and the <select> is named 'newdate'. The
entered date takes the form of M/D/YYYY with no need for leading zeros on
single digit months and days.
Hope it helps.
Javascript code (triggered onBlur of the text field):
function update() {
var newdates = Array(2,3,6);
for(i=0; i<newdates.length; i++) {
var date = document.testform.date.value;
var splits = date.split('/');
splits[0] = newdates[i] + eval(splits[0]);
if(splits[0] > 12) {
splits[0] = splits[0] - 12;
splits[2] = eval(splits[2]) + 1;
}
var reformed_date = splits[0] + '/' + splits[1] + '/' + splits[2];
document.testform.newdate.options[i] = new Option(reformed_date,
reformed_date);
}
} |