Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post

    Removing child elements

    Hiya.

    I'm experimenting with AJAX (www.mithoric.com/new.php), creating a shoutbox but I've run into a problem, other than that I rarely ever code js/dom ever.

    The problem is that I'm creating child elements based on an xml document served from a php script. However everytime it is served I'm just appending all the elements to the end of the child list creating copies. So I decided to just remove all the children each time.. But this doesn't seem to be working.

    Code:
    function remove_shouts(){
    	var shoutbox_div = document.getElementById('shouts');
    	var shouts = shoutbox_div.childNodes;
    	for(var i = 0; i < shouts.length; i++){
    		shoutbox_div.removeChild(shouts.item(i));
    	}
    }
    You can see the entire code in www.mithoric.com/rel/js/ajaxshoutbox.js ..
    Omnis mico antequam dominus Spookster!

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    items.length reflects the current number of items in the collection. removeChild() not only removes a child, but alters the .length property because of this. What I tend to do is the following:

    Code:
    while (parent.hasChildNodes())
        parent.removeChild(parent.firstChild);

  • #3
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Ahh of course, thanks jkd.
    Omnis mico antequam dominus Spookster!

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    If you want to do it at O(1) instead of O(n) you could do it like this instead:
    Code:
    element.parentNode.replaceChild(element.cloneNode(false),element);
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by liorean
    If you want to do it at O(1) instead of O(n) you could do it like this instead:
    Code:
    element.parentNode.replaceChild(element.cloneNode(false),element);
    Slick.


  •  

    Posting Permissions

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