Difference between revisions of "Remove Options from a Combo Box Using JavaScript"
From SmartWiki
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 removes two different [[Types]] available for '''Level 2''' records: | + | The following example removes two different [[Types]] available for new '''Level 2''' records (would be useful if you need to define complex criteria for what type of Level 2 records can be created): |
<pre> | <pre> | ||
function RemoveTypes() | function RemoveTypes() | ||
− | + | { | |
var i=0; | var i=0; | ||
var frm = document.forms[0]; | var frm = document.forms[0]; | ||
// at Level 1 use document.form1 instead of document.forms[0] | // at Level 1 use document.form1 instead of document.forms[0] | ||
+ | |||
+ | if (frm.eventid.value==0) | ||
// Note: this loop starts at the bottom to avoid skipping any due to re-indexing as they are removed. | // Note: this loop starts at the bottom to avoid skipping any due to re-indexing as they are removed. | ||
for (i=frm.eventtype.options.length-1;i>=0;i--) | for (i=frm.eventtype.options.length-1;i>=0;i--) | ||
Line 16: | Line 18: | ||
if (frm.eventtype.options[i].text=='SecondType') frm.eventtype.options[i]=null; | if (frm.eventtype.options[i].text=='SecondType') frm.eventtype.options[i]=null; | ||
} | } | ||
− | + | } | |
} | } | ||
RemoveTypes(); | RemoveTypes(); |
Revision as of 12:05, 23 October 2009
You can use a JavaScript function to modify the options available in a pull-down box on the page.
The following example removes two different Types available for new Level 2 records (would be useful if you need to define complex criteria for what type of Level 2 records can be created):
function RemoveTypes() { var i=0; var frm = document.forms[0]; // at Level 1 use document.form1 instead of document.forms[0] if (frm.eventid.value==0) // Note: this loop starts at the bottom to avoid skipping any due to re-indexing as they are removed. for (i=frm.eventtype.options.length-1;i>=0;i--) { if (frm.eventtype.options[i].text=='FirstType') frm.eventtype.options[i]=null; if (frm.eventtype.options[i].text=='SecondType') frm.eventtype.options[i]=null; } } } RemoveTypes();
Note: the same syntax can be used at Level 1, replacing document.forms[0] with document.form1