Eduski
asked on
select box population with auto select current date
I have 3 select boxes that are populated with the month, day, and year... Jan Feb Mar, 01,02,03 etc etc
Is there a way to auto select the box closest to today's date?
Is there a way to auto select the box closest to today's date?
What language are you using?
ASKER
coldfusion
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi
It can be done both client-side and server-side depending on how you want it and what you have available. I will only show you client-side Javascript right now, but it's of your opinion how you want to proceed.
<script language="javascript">
<!--
var monthArray = new Array("January","February" ,"March"," April","Ma y","June", "July","Au gust","Sep tember","O ctober","N ovember"," December") ;
function selectClosestDate(monthFie ld,dayFiel d,yearFiel d) {
//Here, I'm assuming the year field is a input field, not a drop down. If it is, it can be corrected quite easily.
var now = new Date;
var currDate = now.getDate()+1;
var currMonth = monthArray[now.getMonth()] ;
var currYear = now.getYear();
for (var i=0;i<monthField.options.l ength;i++) {
if (monthField.options[i].val ue==currMo nth) {
monthField.selectedIndex = i;
break;
}
}
for (var i=0;i<dayField.options.len gth;i++) {
if (parseInt(dayField.options [i].text,1 0)==currDa te) {
dayField.selectedIndex = i;
}
}
yearField.value = currYear;
}
Have this function in <body onload> and supply the monthField, dayField, and yearField. For your monthfield, each of the options have to have a value of "January" or "February" like this:
<option value="January">Jan</optio n>
fully written out. Nothing needs to be done for the date field.
Regards,
Zyloch
It can be done both client-side and server-side depending on how you want it and what you have available. I will only show you client-side Javascript right now, but it's of your opinion how you want to proceed.
<script language="javascript">
<!--
var monthArray = new Array("January","February"
function selectClosestDate(monthFie
//Here, I'm assuming the year field is a input field, not a drop down. If it is, it can be corrected quite easily.
var now = new Date;
var currDate = now.getDate()+1;
var currMonth = monthArray[now.getMonth()]
var currYear = now.getYear();
for (var i=0;i<monthField.options.l
if (monthField.options[i].val
monthField.selectedIndex = i;
break;
}
}
for (var i=0;i<dayField.options.len
if (parseInt(dayField.options
dayField.selectedIndex = i;
}
}
yearField.value = currYear;
}
Have this function in <body onload> and supply the monthField, dayField, and yearField. For your monthfield, each of the options have to have a value of "January" or "February" like this:
<option value="January">Jan</optio
fully written out. Nothing needs to be done for the date field.
Regards,
Zyloch
ASKER
beautiful pinal, thanks
Ahh, sorry stale window.
Regards
Regards
ASKER
thanks for your attempts anyway, i appreciate you taking the time
You are welcome. Glad to help you,
Have a good day!
---Pinal
Have a good day!
---Pinal