rows property
The rows property stores a collection of
tr
rows. It can be applied to both the table and
its tHead,
tBodies,
tFoot
sections.
Syntax
table.rows;
Example
Let's loop through all the rows of a table:
<table id="table">
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
<tr>
<td>4</td><td>5</td><td>6</td>
</tr>
<tr>
<td>7</td><td>8</td><td>9</td>
</tr>
</table>
let table = document.querySelector('#table');
for (let row of table.rows) {
console.log(row);
}
Example
Let's loop through all the rows of a table
using the rows property, and in each
row we loop through its cells using the
cells
property:
<table id="table">
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
<tr>
<td>4</td><td>5</td><td>6</td>
</tr>
<tr>
<td>7</td><td>8</td><td>9</td>
</tr>
</table>
let table = document.querySelector('#table');
for (let row of table.rows) {
for (let cell of row.cells) {
console.log(cell);
}
}
Example
We find the number of rows in a table:
<table id="table">
<tr>
<td>1</td><td>2</td><td>3</td>
</tr>
<tr>
<td>4</td><td>5</td><td>6</td>
</tr>
<tr>
<td>7</td><td>8</td><td>9</td>
</tr>
</table>
let table = document.querySelector('#table');
console.log(table.rows.length);
The code execution result:
3