What is the best way to empty a list box?

M

mark4asp

What is the best way to empty a list box, 1, 2 or 3?

1. document.myForm.myListBox.options.length=null;

2. document.myForm.myListBox.options.length=0;

3. for (var i=0; i<document.myForm.myListBox.options.length; i++)
document.myForm.myListBox.options=null;
 
F

Fred

mark4asp said:
What is the best way to empty a list box, 1, 2 or 3?

1. document.myForm.myListBox.options.length=null;

2. document.myForm.myListBox.options.length=0;

3. for (var i=0; i<document.myForm.myListBox.options.length; i++)
document.myForm.myListBox.options=null;


#1 doesn't look like it will work.

#2 works.

#3 will only remove half of them - the collection is live. A better
alternative would be:

var sel = document.myForm.myListBox;
while (sel.firstChild){
sel.removeChild(sel.firstChild);
}

#2 wins on simplicity.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top