Take the table ID as a parameter:
function countColumn(tableID) {
var colCount = 0;
$('#' + tableID + ' tr:nth-child(1) td').each(function () {
if ($(this).attr('colspan')) {
colCount += +$(this).attr('colspan');
} else {
colCount++;
}
});
return colCount;
}
You could also define a jQuery method that operates on a selector:
$.fn.colCount = function() {
var colCount = 0;
$('tr:nth-child(1) td', this).each(function () {
if ($(this).attr('colspan')) {
colCount += +$(this).attr('colspan');
} else {
colCount++;
}
});
return colCount;
};
Then you can do var count = $("#someTable").colCount();