[thelist] Adding items to combo boxes
Warden, Matt
mwarden at mattwarden.com
Fri Jul 20 10:47:00 CDT 2001
<disclaimer>
code is untested
</disclaimer>
function addItem(oOption, oSelect)
{
a_currOptions = oSelect.options;
oSelect.options[0] = oOption;
for (var i=1; i < a_currOptions.length+1; i++)
{
oSelect.options[i] = a_currOptions[i-1];
}
}
Where oOption is the option object you are creating below, and oSelect is
the reference to the select object being modified
(document.myform.myselectbox).
You *may* have to get rid of all the options before you can add them, like
this:
function addItem(oOption, oSelect)
{
a_currOptions = oSelect.options;
for (var m=oSelect.length; m <= 0; m--) // bug workaround
{
oSelect.options[m] = null;
}
oSelect.options[0] = oOption;
for (var i=1; i < a_currOptions.length+1; i++)
{
oSelect.options[i] = a_currOptions[i-1];
}
}
HTH,
--
mattwarden
mattwarden.com
On Jul 20, Dominik Wee had something to say about [thelist] Adding items to...
>Can anybody help with the following problem: I want to add an option to
>the beginning of an existing HTML combo box (select)using JavaScript. I
>know that I can add elements to the _end_ of the list by:
>
> newOptionJobTitle = new Option("select one...", "default");
>
>document.frmSearchBar.JobTitleID.options[document.frmSearchBar.JobTitleI
>D.options.length] = newOptionJobTitle;
>
>but how do I insert options at the _top_ without overwriting the first
>item?
More information about the thelist
mailing list