tags:

views:

25

answers:

2

i have a select tag, with some options

<select id="sel">
     <option>text1</option>
     <option>text2</option>
     <option>text3</option>
     <option>text4</option>
</select>

i want to delete all options except second, i.e i want to get

<select id="sel">
     <option>text2</option>
</select>

i think it must looks something like this

document.getElementById('sel').options.length= 0;

but it deletes all list, so could you help me.


thanks

+1  A: 

You should be able to modify this to do as you want.

function removeChildrenFromNode(node)
{
    if(node === undefined || node ==== null)
    {
        return;
    }

    var len = node.childNodes.length;

    while (node.hasChildNodes())
    {
        node.removeChild(node.firstChild);
    }
}
Nalum
@Nalum are there any function, to delete all childs?i used to write document.getElementById('sel').options.length= 0, is it true?
Syom
I don't think there is a function to remove all child nodes, what you have works fine. The function above should work with all node types.
Nalum
@Nalum thanks man
Syom
+1  A: 
var
    sel = document.getElementById("sel"),
    options = sel.getElementsByTagName("option");
for (var i=options.length-1; i>=2; i--) {
    sel.removeChild(options[i]);
}
sel.removeChild(options[0]);
jimbojw