Difference between revisions of "Add Options to a Combo Box Using JavaScript"
From SmartWiki
Line 4: | Line 4: | ||
<pre> | <pre> | ||
− | function | + | function clearTimes() |
{ | { | ||
− | + | if (document.forms[0].startdate.value=="") | |
+ | { | ||
try | try | ||
{ | { | ||
− | document.forms[0].starthour.add(new Option("", "", true, true), document.forms[0].starthour.options[0]) | + | document.forms[0].starthour.add(new Option("", "", true, true), document.forms[0].starthour.options[0]) //add blank option to beginning of starthour |
+ | document.forms[0].startminute.add(new Option("", "", true, true), document.forms[0].startminute.options[0]) | ||
+ | document.forms[0].endhour.add(new Option("", "", true, true), document.forms[0].endhour.options[0]) | ||
+ | document.forms[0].endminute.add(new Option("", "", true, true), document.forms[0].endminute.options[0]) | ||
} | } | ||
catch(e) | catch(e) | ||
{ //in IE, try the below version instead of add() | { //in IE, try the below version instead of add() | ||
− | document.forms[0].starthour.add(new Option("", "", true, true), 0) | + | document.forms[0].starthour.add(new Option("", "", true, true), 0) //add blank option to beginning of starthour |
+ | document.forms[0].startminute.add(new Option("", "", true, true), 0) | ||
+ | document.forms[0].endhour.add(new Option("", "", true, true), 0) | ||
+ | document.forms[0].endminute.add(new Option("", "", true, true), 0) | ||
} | } | ||
− | |||
} | } | ||
− | + | clearTimes(); | |
</pre> | </pre> | ||
Revision as of 16:37, 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 clearTimes() { if (document.forms[0].startdate.value=="") { try { document.forms[0].starthour.add(new Option("", "", true, true), document.forms[0].starthour.options[0]) //add blank option to beginning of starthour document.forms[0].startminute.add(new Option("", "", true, true), document.forms[0].startminute.options[0]) document.forms[0].endhour.add(new Option("", "", true, true), document.forms[0].endhour.options[0]) document.forms[0].endminute.add(new Option("", "", true, true), document.forms[0].endminute.options[0]) } catch(e) { //in IE, try the below version instead of add() document.forms[0].starthour.add(new Option("", "", true, true), 0) //add blank option to beginning of starthour document.forms[0].startminute.add(new Option("", "", true, true), 0) document.forms[0].endhour.add(new Option("", "", true, true), 0) document.forms[0].endminute.add(new Option("", "", true, true), 0) } } clearTimes();
The options available are:
- document.forms[0].starthour.add(new Option(Display text, Store value, defaultSelected, selected), document.forms[0].starthour.options[0])