Monday, November 14, 2011

Remove n’th table column - jQuery plugin


My usual short Friday post. Today I would like to share with you a new utility I wrote to work with HTML tables. We already have jQuery functions to add table rowremove table row (on user click), add table row number plugin and now here is jQuery code to remove table column.

jQuery utility to remove table’s n’th column:

$.fn.removeCol = function(col){
    // Make sure col has value
    if(!col){ col = 1; }
    $('tr td:nth-child('+col+'), tr th:nth-child('+col+')', this).remove();
    return this;
};
Just add this code to your javascript file and use it in your jQuery code like this:
// Remove all table's second columns
$('table').removeCol(2);
// Remove table's first column (default)
$('table').removeCol();
The function takes column number to delete as an argument and removes that column. If you don’t supply any column number it removes the first table column by default.

0 comments:

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More