 <p>I have just found out the error in my script. The 1st script works fine after correcting the error. I should have used th for the header row as I did in the html and not td. Also I have added the missing .hide() The 2nd script though hides the columns but will still show a space for any column it hides. Note that the correct script had been posted earlier but with toggle buttons, check box etc which didn't suite my desire. This script works fine without checkbox or toggle button. </p> <pre><code>&lt;script&gt; function finish(){ var sdtbl = document.getElementById('sdtbl'); var x = sdtbl.rows[0].cells.length; var y = sdtbl.rows.length; //Hide unused Bonus &amp; Deduction payroll headers for (var i = 0; i &lt; x; i++) { var hdr = sdtbl.rows[0].getElementsByTagName('th')[i]; var val = hdr.innerHTML; if (val === "") { $('#sdtbl tr').eq(0).find('th').eq(i).hide(); for (var j = 1; j &lt; y; j++) { $('#sdtbl tr').eq(j).find('td').eq(i).hide(); } } } } &lt;/script&gt; </code></pre>