jQuery: How to count table columns?

Viewed 88538

Using jQuery, how would you figure out how many columns are in a table?

<script>
    alert($('table').columnCount());
</script>

<table>
    <tr>
        <td>spans one column</td>
        <td colspan="2">spans two columns</td>
        <td colspan="3">spans three columns</td>
    <tr>
</table>

The total number of columns in this example is 6. How could I determine this using jQuery?

16 Answers

One Line:

$('.table-responsive tr th').children().length;
Related