This JS function removed all options in a combo box except the first entry. If you want to remove the first entry as well then change the i>0 to i>=0
function removeExistingOptions(selBox){
var i;
for (i = selBox.length - 1; i>0; i--) {
selBox.remove(i);
}
}
The following function adds options to an existing combo box.
function addOptions(selBox){
var opt1=document.createElement('option');
opt1.text='OPT1';
opt1.value='OPT1';
var opt2=document.createElement('option');
opt2.text='OPT2';
opt2.value='OPT2';
try
{
// standards compliant
selBox.add(opt1,null);
selBox.add(opt2,null);
}
catch(ex)
{
// IE only
selBox.add(opt1);
selBox.add(opt2);
}
}
In both functions you have to pass the select element as the reference to the function.
function removeExistingOptions(selBox){
var i;
for (i = selBox.length - 1; i>0; i--) {
selBox.remove(i);
}
}
The following function adds options to an existing combo box.
function addOptions(selBox){
var opt1=document.createElement('option');
opt1.text='OPT1';
opt1.value='OPT1';
var opt2=document.createElement('option');
opt2.text='OPT2';
opt2.value='OPT2';
try
{
// standards compliant
selBox.add(opt1,null);
selBox.add(opt2,null);
}
catch(ex)
{
// IE only
selBox.add(opt1);
selBox.add(opt2);
}
}
In both functions you have to pass the select element as the reference to the function.
No comments:
Post a Comment