2

The idea is to build a function that takes an input and uses that to build a grid. I'm trying to establish the grid functionality first, and I'm having a peculiar error. I searched for a few hours, but the answers all tell me that a simple "append" should be working.

The specific error that I am getting: When I load up the webpage, it is only adding one table row to the tbody, and only one table data to that table row. The idea is instead to create a grid of 16 x 16, with 16 rows and 16 data. Console logs show that the loops are all working correctly.

The html is just a basic file that imports the javascript correctly (All tested) with a simple: div class="container" /div

Code:

  $(document).ready(function(){
  $(".container").html("");
  /*this function makes a table of size numRow and
  num data. it then gives each data element
  */
  //blank rows to insert
  var blankResults = $("<table>");
  var result = blankResults;
  var row = $("<tr/>");
  var data = $("<td/>");

  function makeTable(num) 
  {
    result = blankResults;
        //create num rows
    for (var i = 0; i < num; i++)
    {
      //for each row
      //add data num times
      for (var j = 0; j < num; j++)
      {
        console.log(j);
        row.append(data);
      }
      //append row
      console.log(i);
      result.append(row);
    }
  }
  //starting area
  makeTable(16);
  $(".container").append(result);
  //Start with 16 by 16 of square divs - 
  //put inside a container div
});
1
  • you are not closing the tr and td tag after creating rows and columnss. thats why you are getting only one row with colum. In the inner for loop close td and in outer for loop close tr. That should solve your problem. Commented Jun 7, 2015 at 9:16

3 Answers 3

2

Try this code.

$(document).ready(function(){
    $(".container").html("");
    /*this function makes a table of size numRow and
          num data. it then gives each data element
          */
    function makeTable(num) 
    {
        var output = '<table>';
        //create num rows
        for (var i = 0; i < num; i++)
        {
            //for each row
            output+= '<tr>'
            for (var j = 0; j < num; j++)
            {
                output += '<td></td>';
            }
            output += '</tr>';

        }
        output += '</table>';
        return output;
    }
    //starting area
    var result = makeTable(16);
    $(".container").append(result);
    //Start with 16 by 16 of square divs - 
    //put inside a container div
 });
Sign up to request clarification or add additional context in comments.

1 Comment

This was precisely what I needed. The other answers were great as well, but this one really clicked for me. Thank you, it was really helpful.
1

You are appending to the same variables all the time...row and 'data`. you should not do that.

As you can see from the code below, you need to create the var row = $("<tr>"); on each loop, to reference it when you append the <td> (table cell) to that newly created row.

Modifed to use only one loop:

$(document).ready(function(){
  function makeTable(num) {
    var table = $("<table>"), row;
    
    for (var i = 0; i < num; i++){
      row = $("<tr>");
      table.append(row);
      row.append(Array(num + 1).join("<td></td>"));
    }
    return table;
  }

  $(".container").html(makeTable(16));
});

DEMO PLAYGROUND


Of course, this is not a good way generating a table. running jQuery function on each loop is slow and bad practice. You should generate a big string which will represent your DOM structure and then append that string where needed and jQuery will make a DOM node out of it.

Comments

0

I made up my own html for this but it should be as simple as using two nested for loops grabbing values the size input. Here's what I came up with:

$("#tableMaker").click(function () {
    $('.container').html("");
    $('.container').append("<table></table>");
    for (var i = 0; i < $('#size').val(); i++) {
        $('table').append("<tr></tr>");
        for (var j = 0; j < $('#size').val(); j++) {
            $('tr:last').append("<td>Column " + (j+1) + ", Row " + (i+1) + "</td>");
        }
    }
})
td {
    border: black solid 1px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="size" placeholder="Size" />
<button id="tableMaker" type="button">Generate Table</button>
<br />
<div class="container">
    <div />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.