Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to do 5x5 table in JQuery?

Hi. I want to do 5x5 table like below in JQuery. Is there a more efficient way to do it? Without copy the same code 5x ?

<table>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

What I have:

// create a table in html document
var $table = $("<table></table>");
$("#table_view").append($table);

//create 5 rows in a table
var amountOfRows = 5;

for(var i=0; i<amountOfRows; i++)
{
    var $row = $("<tr></tr>");
    var $td1 = $("<td></td>");
    var $td2 = $("<td></td>");
    var $td3 = $("<td></td>");
    var $td4 = $("<td></td>");
    var $td5 = $("<td></td>");

    $row.append($td1);
    $row.append($td2);
    $row.append($td3);
    $row.append($td4);
    $row.append($td5);
    $table.append($row);
}

2 Answers

Added some content so you can actually see the table, but I think this is what you are trying to do: jsbin example

What about trying a for loop inside your for loop ? With the same number (5), it could work ?