# Adding rows and columns to an HTML table

Let's say we have some HTML table - `#table`. Let's learn how to add new rows and columns to it.

## Adding rows

Adding rows is not difficult: you need to create `tr`, and then run a loop that will add the desired number of cells to this row (let's say `3`):

`let table = document.querySelector('#table'); let tr = document.createElement('tr'); for (let i = 1; i <= 3; i++) { let td = document.createElement('td'); tr.appendChild(td); } table.appendChild(tr);`

Make a button, by clicking on which a new row will be added to the table.

## Adding columns

But with adding columns, it’s a little more complicated: you need to run a loop that will go through all the table rows and add a new cell to each row:

`let trs = document.querySelectorAll('#table tr'); for (let tr of trs) { let td = document.createElement('td'); tr.appendChild(td); }`

Given a `2` by `2` table:

`<table id="table"> <tr> <td></td> <td></td> </tr> <tr> <td></td> <td></td> </tr> </table>` `td { width: 50px; height: 50px; border: 1px solid black; }`

There is also a button. Make it so that when you click on the button, the table increases by one row and one column.

← →
↑
en ru