469,347 Members | 1,738 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,347 developers. It's quick & easy.

Remove items from a combobox

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)

May 26 '07 #1
1 10343
On May 26, 7:00 pm, SM <servandomont...@gmail.comwrote:
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);
--
Rob

May 26 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Wayne Wengert | last post: by
reply views Thread by tupolev | last post: by
5 posts views Thread by Wally | last post: by
1 post views Thread by Mike | last post: by
1 post views Thread by Gian Paolo | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.