Difference between revisions of "Add Options to a Combo Box Using JavaScript"

From SmartWiki
Jump to: navigation, search
Line 1: Line 1:
 
You can use a JavaScript function to modify the options available in a pull-down box on the page.
 
You can use a JavaScript function to modify the options available in a pull-down box on the page.
  
The following example adds a new blank selection at the top of the ''starthour'' field at '''Level 2''':
+
The following example adds a new blank selection at the top of the ''starthour'' field at '''Level 2''' for new records:
  
 
<pre>
 
<pre>
 
function changeStartHour()
 
function changeStartHour()
 
  {
 
  {
 +
if (document.forms[0].eventid.value==0)
 
   try
 
   try
 
   {
 
   {
Line 14: Line 15:
 
       document.forms[0].starthour.add(new Option("", "", true, true), 0)
 
       document.forms[0].starthour.add(new Option("", "", true, true), 0)
 
   }
 
   }
 +
}
 
}
 
}
 
changeStartHour();
 
changeStartHour();

Revision as of 14:51, 31 July 2009

You can use a JavaScript function to modify the options available in a pull-down box on the page.

The following example adds a new blank selection at the top of the starthour field at Level 2 for new records:

function changeStartHour()
 {
 if (document.forms[0].eventid.value==0)
   try
   {
       document.forms[0].starthour.add(new Option("", "", true, true), document.forms[0].starthour.options[0])
   }
   catch(e)
   { //in IE, try the below version instead of add()
       document.forms[0].starthour.add(new Option("", "", true, true), 0)
   }
 }
}
changeStartHour();

The options available are:

document.forms[0].starthour.add(new Option(Display text, Store value, defaultSelected, selected), document.forms[0].starthour.options[0])