Search code examples
javascripthtml-tabledynamically-generated

Creating html table using Javascript not working


Basically, I want the user the just change the 'height' variable to how ever many rows he wants, and then store the words which each td in the row should contain, and the code should then generate the table.

My html is just this:

    <table id="newTable">
    </table>

This is my Javascript:

<script type="text/javascript">
var height = 2; // user in this case would want 3 rows (height + 1)
var rowNumber = 0;

var height0 = ['HeadingOne', 'HeadingTwo']; // the words in each td in the first row
var height1 = ['firstTd of row', 'secondTd of row']; // the words in each td in the second row
var height2 = ['firstTd of other row', 'secondTd of other row']; // the words in each td in the third row

$(document).ready( function() {
    createTr();
});

function createTr () {
    for (var h=0; h<height + 1; h++) { // loop through 3 times, in this case (which h<3)
        var theTr = "<tr id='rowNumber" + rowNumber + "'>"; // <tr id='rowNumber0'>
        $('#newTable').append(theTr); // append <tr id='rowNumber0'> to the table
        for (var i=0; i<window['height' + rowNumber].length; i++) {
            if (i == window['height' + rowNumber].length-1) { // if i==2, then that means it is the last td in the tr, so have a </tr> at the end of it
                var theTd = "<td class='row" + rowNumber + " column" + i + "'>" + window['height' + rowNumber][i] + "</td></tr>";
                $('#rowNumber' + rowNumber).append(theTr); // append to the end of the Tr

            } else {
                var theTd = "<td class='row" + rowNumber + " column" + i + "'>" + window['height' + rowNumber][i] + "</td>";
                $('#rowNumber' + rowNumber).append(theTr);
            }
        }
        rowNumber += 1;
    }
}
</script>

I did 'alert(theTr);' and 'alert(theTd);' and they looked correct. How come this code doesn't generate any table?


Solution

  • All that window["height"+rowNumber] stuff is a poor way to do it. Use an array, and pass it as a parameter to the function so you don't use global variables. And use jQuery DOM creation functions instead of appending strings.

    <script type="text/javascript">
    var heights = [['HeadingOne', 'HeadingTwo'], // the words in each td in the first row
                   ['firstTd of row', 'secondTd of row'], // the words in each td in the second row
                   ['firstTd of other row', 'secondTd of other row'] // the words in each td in the third row
                  ];
    
    $(document).ready( function() {
        createTr(heights);
    });
    
    function createTr (heights) {
        for (var h=0; h<heights.length; h++) { // loop through 3 times, in this case (which h<3)
            var theTr = $("<tr>", { id: "rowNumber" + h});
            for (var i=0; i<heights[h].length; i++) {
                theTr.append($("<td>", { "class": "row"+h + " column"+i,
                                         text: heights[h][i]
                                       }));
            }
            $('#newTable').append(theTr); // append <tr id='rowNumber0'> to the table
        }
    }
    </script>
    

    JSFIDDLE