Difference between revisions of "Add Options to a Combo Box Using JavaScript"
(5 intermediate revisions by one other user not shown) | |||
Line 6: | Line 6: | ||
function clearTimes() | function clearTimes() | ||
{ | { | ||
− | + | var frm = document.forms[0]; | |
+ | // at Level 1 use document.form1 instead of document.forms[0] | ||
+ | |||
+ | if (frm.startdate.value=="") //this will only run if there is not startdate already saved | ||
{ | { | ||
try | try | ||
{ | { | ||
− | + | frm.starthour.add(new Option("", "", true, true), frm.starthour.options[0]) //add blank option to beginning of starthour | |
− | + | frm.startminute.add(new Option("", "", true, true), frm.startminute.options[0]) | |
− | + | frm.endhour.add(new Option("", "", true, true), frm.endhour.options[0]) | |
− | + | frm.endminute.add(new Option("", "", true, true), frm.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() | ||
− | + | frm.starthour.add(new Option("", "", true, true), 0) //add blank option to beginning of starthour | |
− | + | frm.startminute.add(new Option("", "", true, true), 0) | |
− | + | frm.endhour.add(new Option("", "", true, true), 0) | |
− | + | frm.endminute.add(new Option("", "", true, true), 0) | |
} | } | ||
+ | } | ||
} | } | ||
clearTimes(); | clearTimes(); | ||
+ | |||
</pre> | </pre> | ||
Line 29: | Line 34: | ||
:''document.forms[0].starthour.add(new Option('''''Display text, Store value, defaultSelected, selected'''''), document.forms[0].starthour.options[0]) '' | :''document.forms[0].starthour.add(new Option('''''Display text, Store value, defaultSelected, selected'''''), document.forms[0].starthour.options[0]) '' | ||
+ | |||
+ | '''Note:''' the same syntax can be used at Level 1, replacing '''document.forms[0]''' with '''document.form1''' | ||
+ | |||
+ | ==See Also== | ||
+ | * [[Remove Options from a Combo Box Using JavaScript]] | ||
+ | * [[Conditional Lists of Values]] | ||
[[Category:JavaScript]] | [[Category:JavaScript]] |
Latest revision as of 11:30, 12 June 2014
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, startminute, endhour and endminute fields at Level 2 for new records:
function clearTimes() { var frm = document.forms[0]; // at Level 1 use document.form1 instead of document.forms[0] if (frm.startdate.value=="") //this will only run if there is not startdate already saved { try { frm.starthour.add(new Option("", "", true, true), frm.starthour.options[0]) //add blank option to beginning of starthour frm.startminute.add(new Option("", "", true, true), frm.startminute.options[0]) frm.endhour.add(new Option("", "", true, true), frm.endhour.options[0]) frm.endminute.add(new Option("", "", true, true), frm.endminute.options[0]) } catch(e) { //in IE, try the below version instead of add() frm.starthour.add(new Option("", "", true, true), 0) //add blank option to beginning of starthour frm.startminute.add(new Option("", "", true, true), 0) frm.endhour.add(new Option("", "", true, true), 0) frm.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])
Note: the same syntax can be used at Level 1, replacing document.forms[0] with document.form1