I would recommend adding a class to each th and td (something like "col_1", "col_2" etc) and using $("td").children("span:empty") to find the columns that should be hidden.
$(document).ready(function() {
hideEmptyCols($("#mytable"));
});
function hideEmptyCols(table) {
//count # of columns
var numCols = $("th", table).length;
for ( var i=1; i<=numCols; i++ ) {
var empty = true;
//grab all the <td>'s of the column at i
$("td:nth-child(" + i + ")", table).each(function(index, el) {
//check if the <span> of this <td> is empty
if ( $("span", el).text() != "" ) {
empty = false;
return false; //break out of each() early
}
});
if ( empty ) {
$("td:nth-child(" + i + ")", table).hide(); //hide <td>'s
$("th:nth-child(" + i + ")", table).hide(); //hide header <th>
}
}
}
Or (simpler):
function hideEmptyCols(table) {
var rows = $("tr", table).length-1;
var numCols = $("th", table).length;
for ( var i=1; i<=numCols; i++ ) {
if ( $("span:empty", $("td:nth-child(" + i + ")", table)).length == rows ) {
$("td:nth-child(" + i + ")", table).hide(); //hide <td>'s
$("th:nth-child(" + i + ")", table).hide(); //hide header <th>
}
}
}