Remove items from a combobox

S

SM

Hello,
Im creating a combobox using the Javascript and the DOM.

If the combobox exists, i don't want to create another one. I want to
empty all the values and put new values.

I was able to create the combox and create the lines of code that
deletes the values in the combobox (well almost)

I just need help finishing my function

Thanks
Marco



function show_catalog()
{
...
if (???combobox doesnt exist, create one)
{
form = document.createElement("form");

selectElem = document.createElement("select");
selectElem.setAttribute("name", "video");
selectElem.setAttribute("size", "1");

form.appendChild(selectElem);
...
}

else //empty existing one
{
while(???.options.length) {
???.remove(0);
}
}


//ok now fill the newly created combobox or the existing one now
empty
for (var x in catalog)
{
optionElem = document.createElement('option');
optionElem.setAttribute('value', i);

optionElemValue = document.createTextNode(x);
optionElem.appendChild(optionElemValue)

selectElem.appendChild(optionElem);
}

}


The first time, its always going to be empty, therefore its going to
create one (that the reference i will use to empty it when i click)
 
R

RobG

Hello,
Im creating a combobox using the Javascript and the DOM.

If the combobox exists, i don't want to create another one. I want to
empty all the values and put new values.

I was able to create the combox and create the lines of code that
deletes the values in the combobox (well almost)

I just need help finishing my function

Thanks
Marco

function show_catalog()
{
...
if (???combobox doesnt exist, create one)
{
form = document.createElement("form");

selectElem = document.createElement("select");
selectElem.setAttribute("name", "video");
selectElem.setAttribute("size", "1");

form.appendChild(selectElem);
...
}

else //empty existing one
{
while(???.options.length) {
???.remove(0);
}

Replace the above with:

} else {
selectElem.options.length = 0;
}

//ok now fill the newly created combobox or the existing one now
empty
for (var x in catalog)
{
optionElem = document.createElement('option');
optionElem.setAttribute('value', i);

optionElemValue = document.createTextNode(x);
optionElem.appendChild(optionElemValue)

selectElem.appendChild(optionElem);

Replace the above with:

selectElem.options[selectElem.options.length] =
new Option(optText, optValue);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top