Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

appendChild in for loop only adds 1 child

In JavaScript I am creating a grid (the type of grid you see in Photoshop) with HTML tables. The grid size is going to be variable, i.e., changeable by the user, so the size of each grid square must be calculated and divided by the number of pixels available to get an exact size grid.

I've done all this, but I have a problem adding in the necessary table elements to create the grid. My code is in full working order, except when I use the appendChild() function inside a for loop it only ever appends a single child, when it should be appending up to a couple of hundred.

My code:

grid.show = function(event)
{
    var e = event;

    if(grid.show_grid == false)
    {
        grid.show_grid = true;

        parent.document.getElementById("grid_table").style.display = "block";

        // Get grid (table) and create some elements.
        grid.get_grid = parent.document.getElementById("grid_table");
        grid.tr       = parent.document.createElement("tr");
        grid.td       = parent.document.createElement("td");

        // Clear the grid of all squares so we don't have to worry about subtracting anything.
        grid.get_grid.innerHTML = "";

        // Calculate the number of horizontal and vertical squares.
        var horizontal = Math.ceil(grid.total_width / grid.size);
        var vertical   = Math.ceil(grid.total_height / grid.size);

        // This was a nested loop, removed for demonstration.
        // Attempting to add 10 "<tr><td></td></tr>" to the table.
        for(var j = 0; j < 10; j++)
        {
            grid.tr.appendChild(grid.td);
        }

        //console.log(grid.tr);

        // Add the elements to the table.
        grid.get_grid.appendChild(grid.tr);

    }
    else
    {   
        grid.show_grid = false;
        parent.document.getElementById("grid_table").style.display = "none";
    }
}

This only ever returns a single table row with single table data inside, like so:

<tr>
    <td></td>
</tr>

I've already looked at this page and this page, and they sound promising but I just can't figure out how to make this work.

EDIT: Code now working, solution:

grid.show = function(event)
{
    var e = event;

    if(grid.show_grid == false)
    {
        grid.show_grid = true;

        parent.document.getElementById("grid_table").style.display = "block";

        grid.get_grid           = parent.document.getElementById("grid_table");
        grid.tr                 = null;
        grid.td                 = null;
        grid.get_grid.innerHTML = "";

        var horizontal = Math.ceil(grid.total_width / grid.size);
        var vertical   = Math.ceil(grid.total_height / grid.size);

        for(var i = 0; i < vertical; i++)
        {
            grid.tr = parent.document.createElement("tr");

            for(var j = 0; j < horizontal; j++)
            {
                grid.td = parent.document.createElement("td");

                grid.td.width = grid.size;
                grid.td.height = grid.size;

                grid.tr.appendChild(grid.td);
            }
            grid.get_grid.appendChild(grid.tr);
        }
    }
    else
    {   
        grid.show_grid = false;
        parent.document.getElementById("grid_table").style.display = "none";
    }
}
like image 947
user1649326 Avatar asked Oct 04 '12 15:10

user1649326


People also ask

What is the child object appended in the appendChild () method?

appendChild() The appendChild() method of the Node interface adds a node to the end of the list of children of a specified parent node. If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position.

What is the difference between appendChild and append?

append() can append several nodes and strings, whereas Node. appendChild() can only append one node.

Why is appendChild not a function?

The "appendChild is not a function" error occurs for multiple reasons: calling the appendChild() method on a value that is not a DOM element. placing the JS script tag above the code that declares the DOM elements. misspelling appendChild - it's case sensitive.


2 Answers

You are appending the same element over and over. You need to call document.createElement each time you wish to have a new element.

EDIT: If the element setup is really complicated and potentially includes children then you can also use Node.cloneNode

like image 117
TheZ Avatar answered Sep 21 '22 14:09

TheZ


If you want to make one copy of the element, you will need to clone it. Use cloneNode()

So change

grid.tr.appendChild(grid.td);

to

grid.tr.appendChild(grid.td.cloneNode(true));
like image 40
epascarello Avatar answered Sep 19 '22 14:09

epascarello