Search code examples
javascripthtml-table

Count number of columns in a table row


I have a table similar to:

<table id="table1">
<tr>
  <td><input type="text" value="" /></td>
  <td><input type="text" value="" /></td>
  <td><input type="text" value="" /></td>
  <td><input type="text" value="" /></td>
</tr>
<tr>
  <td><input type="text" value="" /></td>
  <td><input type="text" value="" /></td>
  <td><input type="text" value="" /></td>
  <td><input type="text" value="" /></td>
</tr>
<table>

I want to count the number of td element in a row. I am trying:

document.getElementById('').cells.length;
document.getElementById('').length;
document.getElementById('').getElementsByTagName('td').length;

It did not show actual result.


Solution

  • document.getElementById('table1').rows[0].cells.length
    

    cells is not a property of a table, rows are. Cells is a property of a row though