I've come across a bit of a conundrum. Firefox handles DOM manipulation based on the standard which (would'a'thunk-it?) works. However, Internet Explorer does not. So here's the situation.

I've got a select box and a remove button which, in Firefox, works as advertised, ie., removes the selected element from the list box and destroys the parent if the container optgroup becomes empty using the removeChild() method. Internet Exploer does not like this method for some reason so I must use the removeNode() method. This is all well and good except for the fact that when using removeNode() you may only remove the first child element. For example:

Select Box:
   OptGroup 1:
      Option 1  <---IE can only remove this element
      Option 2  <---Firefox can remove this, IE cannot
End Select Box

So, in essence, my question is: Has anyone found a way, in IE, to be able to remove a random element (and my random I mean not the first child) from a list of child nodes?

This is my current code

function removeSelected(parentElement)
	if(parentElement.id == null)
		parentElement = document.getElementById(parentElement);
		child = parentElement.childNodes[i];
				try/*standard compliant*/{
				catch/*IE crap*/(ex){
		else if(child.firstChild.nodeType == Node.ELEMENT_NODE && child.hasChildNodes())
9 Years
Discussion Span
Last Post by ShawnCplus

> Internet Exploer does not like this method for some reason
Why? What made you reach that conclusion? In fact, my entire snippet for removing the selected option relies on removeChild and it works fine in IE/FF/Opera.

Also a few things to keep in mind.

  • Post the entire code which we can copy and run instead of just the relevant function which in turn requires us to develop a test case.
  • Don't use tabs for indentation, at least when posting code. It makes a mess out of it as you can very well see.

Here is a small snippet which should get you going:

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript">
    function doDelete(frm) {
        var sel = frm && frm.elements["selBox"];
        /* empty / non-existent select box */
        if(!sel || sel.selectedIndex < 0) {
        var item = sel.options[sel.selectedIndex];
        var parent = item.parentNode;
        /* remove all the text nodes */
        while(parent.hasChildNodes()) {
            if(parent.childNodes[0].nodeType != 3) {
                /* non-text node found, exit the function */
        /* since we have reached here, the parent node
           must have zero elements so remove it */
    <form action="#" name="frm">
    <select name="selBox">
        <optgroup label="Swedish Cars">
        <option value ="volvo">Volvo</option>
        <option value ="saab">Saab</option>
        <optgroup label="German Cars">
        <option value ="mercedes">Mercedes</option>
        <option value ="audi">Audi</option>
    <input type="button" value="Delete Selected" onclick="doDelete(this.form);">

Yeah, I copied from Vim, and due to laziness haven't changed the tabwidth to 4. Anyway, aside from var sel = frm && frm.elements["selBox"]; being very neat since I haven't seen an inline check for initialization like that before, I figured out the problem. Sending the parent node as an argument was completely unnecessary given that when I was calling it I was just using the child.parentNode anyway (stupid) so instead I'm sending the child as an argument and deriving the parent from that instead of the other way around.

I probably thought removeChild didn't work because of the fact that at one point during development removeChild didn't work in IE but removeNode did *shrug*.

Thanks for that, that really had me stumped.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.