By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,848 Members | 3,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,848 IT Pros & Developers. It's quick & easy.

Removing all but first item in drop down list

P: n/a
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.len gth 0) {
document.getElementById("mycombo").removeChild(doc ument.getElementById("mycombo").childNodes[0])
}

Many thanks,

Chris
Oct 23 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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
Oct 23 '08 #2

P: n/a
var oplist=document.getElementById("mycombo");
for(var i = oplist.options.length-1;i>0;i--)
{
oplist.removeChild(oplist.options[i]);
}
Oct 23 '08 #3

P: n/a
On 2008-10-23 22:58, Chris wrote:
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.len gth 0) {
document.getElementById("mycombo").removeChild(doc ument.getElementById("mycombo").childNodes[0])
}
document.getElementById("mycombo").options.length = 1;
- Conrad
Oct 23 '08 #4

P: n/a
On Oct 23, 10:58 pm, Chris <matchett...@googlemail.comwrote:
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.len gth 0) {
document.getElementById("mycombo").removeChild(doc ument.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);

--
Jorge.
Oct 23 '08 #5

P: n/a
I think
document.getElementById("mycombo").length = 1;
will be more than enough.
Oct 24 '08 #6

P: n/a
I think
document.getElementById("mycombo").length = 1;
will be more than enough.

Oct 24 '08 #7

P: n/a
On 24 Oct, 08:21, Amrit Ranjan <amri...@gmail.comwrote:
I think
* * * * * * document.getElementById("mycombo").length = 1;
will be more than enough.
Didn't get it at first Amrit but thanks, very clever!

Chris
Oct 24 '08 #8

P: n/a
Conrad Lender meinte:
On 2008-10-23 22:58, Chris wrote:
>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.len gth 0) {
document.getElementById("mycombo").removeChild(do cument.getElementById("mycombo").childNodes[0])
}

document.getElementById("mycombo").options.length = 1;
Will this free the ressources occupied by the now discarded options, too?

Gregor
Oct 25 '08 #9

P: n/a
On 2008-10-25 23:10, Gregor Kofler wrote:
>document.getElementById("mycombo").options.leng th = 1;

Will this free the ressources occupied by the now discarded options,
too?
I guess that would depend on the implementation. They should be freed
(sooner or later) by a reasonably designed scripting engine, same as
with removeChild(). In IE, on the other hand, there's still the GC
problem with circular references between JScript and host objects, so
there's at least one implementation that won't free the resources in
certain cases. removeChild() wouldn't help there either.
- Conrad
Oct 25 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.