0

So I'm trying to work on a JavaScript function that will do what I said in the title. I assume the skeleton of it will be something like:

    function createInputTable(divID, tableID, rows, cols)
    {
                            var T = document.createElement('table id="'+tableID+'"');

                            // ... Do some stuff ...

            document.getElementById(divID).appendChild(T);
    }

Now I just need to figure out what goes in the // ... Do some stuff ....

From what I understand, appendChild(n) finds the "lowest" child node and adds a node n "below" it. Since my table consists of children that are rows, which have children that are columns, each of which has 1 child that is an input cell, I should have a loop like the following.

            for (var i = 0; i < rows; ++i)
            {
                var thisRow = document.createElement("tr");
                for (var j = 0; j < cols; ++j)
                {
                    var thisCol = document.createElement("td");
                    var thisCell = document.createElement('input type="text"');
                    thisCol.appendChild(thisCell);
                    thisRow.appendChild(thisRow);
                }
                document.getElementById.appendChild(thisRow);
            }

Or not. I'm wondering whether my understanding is correct and, if not, what I need to fix.

  • Just saying: http://stackoverflow.com/questions/83073/why-not-use-tables-for-layout-in-html maybe this help you: http://stackoverflow.com/questions/14643617/create-table-using-javascript?rq=1 – Francisco Corrales Morales Apr 01 '14 at 17:29
  • Easy enough to test in jsfiddle, have you tried it? you seem to be on the right track in general. – vector Apr 01 '14 at 17:38
  • Where did you get the _appendChild(n) finds the "lowest" child node and adds a node n "below" it_ from? The collection of child nodes of an element is just a simple array; the function doesn't have to "find" anything. It just adds a new node to the end of the array, no searching involved. – Mr Lister Apr 02 '14 at 06:07
  • @vector For testing your own Javascript routines, JSFiddle might be overkill. All you need is Notepad and a browser. – Mr Lister Apr 02 '14 at 06:11
  • @mr lister, it didn't get him(her) very far did it? Now he's(she) here, had the fiddle been done, it would have been that much easier to help, just saying – vector Apr 02 '14 at 16:58

0 Answers0