You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jacob Wilson <st...@yahoo.com> on 2003/11/05 01:05:48 UTC

Javascript question...

Hi Folks...
One more javascript question...
 
I have a page that consists of date fields as select boxes... say optionDay, optionMonth, optionYear... I fill the selectboxes calling a javascript function... 
 
Now if I have values of day, month, year executing an action. How can I default those values in the select box??? I mean how can I make that particular option 'selected'??? I know we can do that by calling a funtion on the load event...
 
But, I don't want to do it that way as my body tag is generic across all pages...
I am trying something like this...
 
<script language="javascript">
 function generateYear(optionName,defaultValue) {
  var s = document.forms[0].elements[optionName];
  var today = new Date();
  var year = today.getYear();
  document.write('<select NAME='+optionName+'>');
  document.write('<option value="year">Year</option>');
  for (var i=(year-4);i<(year+4);i++ ) {
   document.write('<option value=' + i +'>' + i+'</option>');
  }
  document.write('</select>');
 }

 function generateDay(optionName,defaultValue){
  document.write('<select NAME='+optionName+'>');
  document.write('<option value="day">Day</option>');
  for (var i=1;i<=31;i++ ) {
   document.write('<option value=' + i +'>' + i+'</option>');
  }
  document.write('</select>');
 }

 function generateMonth(optionName,defaultValue){
  document.write('<select NAME='+optionName+'>');
  document.write('<option value="month">Mon</option>');
  document.write('<option value="01">Jan</option>');
  document.write('<option value="02">Feb</option>');
  document.write('<option value="03">Mar</option>');
  document.write('<option value="04">Apr</option>');
  document.write('<option value="05">May</option>');
  document.write('<option value="06">Jun</option>');
  document.write('<option value="07">Jul</option>');
  document.write('<option value="08">Aug</option>');
  document.write('<option value="09">Sep</option>');
  document.write('<option value="10">Oct</option>');
  document.write('<option value="11">Nov</option>');
  document.write('<option value="12">Dec</option>');
  document.write('</select>');
 }
</script>
 
and in my html...
 
I say 
 
<script language="javascript">
 generateMonth("optStartDtMonth",12);
 generateDay("optStartDtDay",05);  
 generateYear("optStartDtYear",2000);
</script>
 
I want to make 12, 05, 2000 selected on the load of the page... how do I do that???
 
I am trying doing something like this...
var s = document.forms[0].elements[optionName];
if ( s.options[i].value == defaultValue) {
   s.options[i].selected = true; 
}
 
On the click of some thing it works -- if I put the code in the generate year or month or day, if fails to recognize the form element!!!
 
Any suggestions appreciated...
 
Thanks in advance...
-Jacob


---------------------------------
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard