471,831 Members | 839 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,831 software developers and data experts.

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 10415
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 YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.