Removing all but first item in drop down list

C

Chris

How do I amend the following code to skip removing the first option in
my dropdown list i.e. I want the first option "Please select..." to
stay?

while (document.getElementById("mycombo").childNodes.length > 0) {
document.getElementById("mycombo").removeChild(document.getElementById("mycombo").childNodes[0])
}

Many thanks,

Chris
 
G

Gregor Kofler

Chris meinte:
How do I amend the following code to skip removing the first option in
my dropdown list i.e. I want the first option "Please select..." to
stay?

Untested:

var e = document.getElementById("mycombo");
while (e.childNodes.length > 1) {
e.removeChild(e.lastChild);
}

Gregor
 
L

liamgegan

var oplist=document.getElementById("mycombo");
for(var i = oplist.options.length-1;i>0;i--)
{
oplist.removeChild(oplist.options);
}
 
J

Jorge

How do I amend the following code to skip removing the first option in
my dropdown list i.e. I want the first option "Please select..." to
stay?

while (document.getElementById("mycombo").childNodes.length > 0) {
document.getElementById("mycombo").removeChild(document.getElementById("myc ombo").childNodes[0])
}

var e= document.getElementById("mycombo");
while (e.firstChild !== (d= e.lastChild)) { e.removeChild(d); }


If there are many children this might be faster:

var e= document.getElementById("mycombo"), d= e.firstChild;
e.innerHTML= "";
e.appendChild(d);
 
A

Amrit Ranjan

I think
document.getElementById("mycombo").length = 1;
will be more than enough.
 
A

Amrit Ranjan

I think
document.getElementById("mycombo").length = 1;
will be more than enough.
 
C

Chris

I think
            document.getElementById("mycombo").length = 1;
will be more than enough.

Didn't get it at first Amrit but thanks, very clever!

Chris
 
G

Gregor Kofler

Conrad Lender meinte:
How do I amend the following code to skip removing the first option
in my dropdown list i.e. I want the first option "Please select..."
to stay?

while (document.getElementById("mycombo").childNodes.length > 0) {
document.getElementById("mycombo").removeChild(document.getElementById("mycombo").childNodes[0])
}

document.getElementById("mycombo").options.length = 1;

Will this free the ressources occupied by the now discarded options, too?

Gregor
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top