Reputation: 1842
Let's say we have a table like this
<table>
<tr>
<th>TH</th>
<td>TD</td>
<td>TD</td>
<td>TD</td>
</tr>
</table>
If I want to get the amount of columns in JS I can simply do something like
var columnCount = tableElement.tBodies[0].rows[0].cells.length
In this case the output is 4. Is there any way to find out how many <th>
are inside a table row, so that the output for that example is 3?
Upvotes: 0
Views: 2142
Reputation: 506
var thCount = tableElement.tBodies[0].rows[0].getElementsByTagName('th').length
or if you want only td's:
var tdCount = tableElement.tBodies[0].rows[0].getElementsByTagName('td').length
Upvotes: 6
Reputation: 21272
It's as simple as:
var columnCount = tableElement.tBodies[0].rows.length
This is to get all the rows.
Upvotes: 0