SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy problem with removeChild in IE

    hi,
    im trying to remove LI from one UL and adding it in another. my code works fine in FF but not in IE. please can u tell me what im doing wrong. the problem is that it does not remove the node.

    function removeNode(nodeId) {
    var ul = document.getElementById("boxes");
    var myLi = document.getElementById(nodeId);

    myLi.removeChild(document.getElementById("img_"+nodeId));
    myLi.removeChild(document.getElementById("br_"+nodeId));
    ul.removeChild(document.getElementById(nodeId));

    var topUl = document.getElementById("boxes1");
    myLi.className = "box1";
    myLi.onclick = function(){moveChild(nodeId)};
    topUl.appendChild(myLi);
    }
    Last edited by hawkseye; Feb 15, 2008 at 10:00.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure exactly what you're trying to do, but here's an example that works:
    Code:
    <html>
    <head>
    <script>
    function moveTo(what, toWhere) {
    	// get a reference to the new container
    	var newUL = document.getElementById(toWhere);
    	
    	// do stuff with the LI, like take out imgs or whatever
    	what.removeChild(what.getElementsByTagName("img")[0]);
    	what.removeChild(what.getElementsByTagName("br")[0]);
    	
    	// remove the onclick function, maybe change it?
    	what.onclick = null;
    	
    	// append it to the new container, note that this will remove the node from its original container
    	newUL.appendChild(what);
    }
    
    </script>
    </head>
    <body>
    <ul id="origin" style="background:gray;height: 50px;">
    	<li onclick="moveTo(this, 'destination');"><img src="something" />node1<br /></li>
    	<li onclick="moveTo(this, 'destination');"><img src="something" />node2<br /></li>
    </ul>
    <ul id="destination" style="background:orange;height: 50px;">
    </ul>
    </body>
    </html>

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,701
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    I think that he's trying to move and LI with all of its content intact.

    When you append an existing node it will automatically be removed from the old location.

    http://www.w3.org/TR/REC-DOM-Level-1...-one-core.html
    appendChild
    Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed.
    Last edited by paul_wilkins; Feb 15, 2008 at 18:48.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •