Main menu:

Site search

Categories

Archive

Removing Children

Sometimes you just have to get rid of children. I have a table with tabular data in it and a title row at the top. When a button is clicked, I need the table to remove all rows that are not the first one (the one with the column titles). I wrote a function called RemoveAllChildren() that removes every row except for the first one. (Yes, I know the function name is a lie!) It took me longer than I thought it would so I thought I would share the code:

function RemoveAllChildren(id)
{
    var table = document.getElementById(id);
    var tbody = table.getElementsByTagName("tbody");

    while(tbody[0].childNodes.length > 1)
    {
        if(tbody[0].childNodes[1].innerHTML != "undefined")
        {
            tbody[0].removeChild(tbody[0].childNodes[1]);
        }
    }
}

Comments

Comment from Nalod
Time: April 12, 2005, 9:09 pm

I like getting rid of children…its a fun game! All you have to do is tell them you are playing Hide and Go Seek and that you will count. Count to 50 and poof the children are gone!!! It really works

–Joe

Comment from Coley
Time: April 13, 2005, 4:41 pm

A table with tabular data in it?! NOOO! Surely not! NO!

I cannot belieeee!

Write a comment