Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery remove last table column

I currently have a table that can be N columns and N rows. In the interface I have a button to remove the last column, but I can't figure out how to remove the last td from all the rows, what I've achieved so far is to remove the first row tdand the last row td, but leave the others there.

Here is my code (only deletes the last title currently):

function removeTableColumn() {
    /*
        removeTableColumn() - Deletes the last column (all the last TDs).
    */
    $('#tableID thead tr th:last').remove(); // Deletes the last title
    $('#tableID tbody tr').each(function() {
        $(this).remove('td:last'); // Should delete the last td for each row, but it doesn't work
    });
}

Am I missing something?

like image 233
CastleDweller Avatar asked Aug 28 '11 14:08

CastleDweller


People also ask

How to remove last column from table using jQuery?

find("td"). last(). remove();

How to remove last column in html table?

function deleteLastColumn() { $("#theadID tr th:not(:last-child)...... $("#tbodyID tr td:not(:last-child)...... }

How do I hide the last column?

To hide a column simply right click on the column and select Hide. Note, you can show any hidden column by right clicking any of the visible columns and choose 'Show hidden data'. Alternatively, you can go to the analysis menu and select 'Reveal Hidden Data'.

How to hide last td in jQuery?

$(trIndex). remove("td:last-child");


1 Answers

That's because the :last selector only matches the last element in the set.

You might be looking for :last-child, which matches the elements that are the last child of their parent:

$("#tableID th:last-child, #tableID td:last-child").remove();
like image 97
Frédéric Hamidi Avatar answered Sep 28 '22 10:09

Frédéric Hamidi