I have a table that is populated from a JSON object. The result is a table with an uneven number of cells in each row. I’m working on a script to automatically append empty cells to each row so that there is a uniform number. My problem is figuring out the length of the longest row in the table. Below is my attempt at this. However, I keep getting errors that the row is undedefined (e.g. “cannot read property length of undefined”). What have I done wrong?
function countCells() {
var columnCount;
$("#MarketsTable tr").each(function () {
columnCount = Math.max(columnCount, $(this).cells.length);
})
return columnCount;
}
You’re mixing basic DOM/JavaScript with jQuery.
Replace
$(this).cells.lengthwith either$(this).children('td').lengthorthis.cells.lengthhttp://api.jquery.com/children/