Proprietà rows

La proprietà rows memorizza una collezione di righe tr. Può essere applicata sia alla tabella che alle sue sezioni tHead, tBodies, tFoot.

Sintassi

javascripttabella.rows;

Esempio

Iteriamo tutte le righe della tabella:

html<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>
javascriptlet table = document.querySelector('#table');

for (let row of table.rows) {
  console.log(row);
}

Esempio

Iteriamo tutte le righe della tabella utilizzando la proprietà rows, e in ogni riga iteriamo le sue celle utilizzando la proprietà cells:

html<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>
javascriptlet table = document.querySelector('#table');

for (let row of table.rows) {
  for (let cell of row.cells) {
    console.log(cell);
  }
}

Esempio

Scopriamo il numero di righe della tabella:

html<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>
javascriptlet table = document.querySelector('#table');
console.log(table.rows.length);

Risultato dell'esecuzione del codice:

javascript3

Vedi anche

  • proprietà cells,
    che contiene le celle della tabella
  • proprietà tHead,
    che contiene il thead della tabella
  • proprietà tFoot,
    che contiene il tfoot della tabella
  • proprietà tBodies,
    che contiene i tbody della tabella