How do I get the column count of a table when there are cells with rowspan/colspan? <p>How do I get the column count of a table when there are cells with rowspan/colspan?</p> <p><strong>UPDATE:</strong> In this question I mean the classical (as far as I know) use of tables, when it's necessary to use the <code>colspan</code>, though it's not required by the specification (and table will look ugly but it will be valid).</p> <p>I need JavaScript/jQuery to get <code>11</code> for the following table (as 11 is the maximum number of columns for this table):</p> <pre><code>&lt;table border="1"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th rowspan="3"&gt;Lorem&lt;/th&gt; &lt;th rowspan="3"&gt;ipsum&lt;/th&gt; &lt;th rowspan="3"&gt;dolor&lt;/th&gt; &lt;th colspan="4"&gt;sit&lt;/th&gt; &lt;th colspan="4"&gt;amet&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th colspan="3"&gt;consectetur&lt;/th&gt; &lt;th rowspan="2"&gt;adipisicing&lt;/th&gt; &lt;th rowspan="2"&gt;elit&lt;/th&gt; &lt;th rowspan="2"&gt;sed&lt;/th&gt; &lt;th rowspan="2"&gt;do&lt;/th&gt; &lt;th rowspan="2"&gt;eiusmod&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;tempor&lt;/th&gt; &lt;th&gt;incididunt&lt;/th&gt; &lt;th&gt;ut&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt;&lt;/tbody&gt; &lt;/table&gt; </code></pre> <p><a href="https://jsfiddle.net/toahb3a3/" rel="nofollow">https://jsfiddle.net/toahb3a3/</a></p> <p>My personal solution is:</p> <pre><code>$(function(){ var max = 0; $('tr').each(function(){ var current = 0; $(this).find('th').each(function(){ var colspan = $(this).attr('colspan'); current += Number(colspan ? colspan : 1); }); max = Math.max(current, max); }); console.log(max); }); </code></pre> <p><a href="https://jsfiddle.net/mt7qhbqd/" rel="nofollow">https://jsfiddle.net/mt7qhbqd/</a></p> <p><strong>UPDATE:</strong> To solve this problem we need the sum of <code>th</code> elements plus their <code>colspan</code> in the very first row of <code>thead</code>. Thanks to Andy for his code, it's really compact and it gave me the thought that I need only the first row. Also, thanks to Quentin who pointed me to the case when <code>colspan</code> is not necessarily used, so Andy's alogrithm won't work (but Quentin's will). Also thanks to Quentin for helping me with the title, as my English isn't good enough.</p> <p><em>Can anybody explain why my question is voted negatively? What's wrong with it</em></p>