How to delete the last column in an HTML TABLE using by jquery?

html-table, javascript, jquery

Solution

Try

$('#persons tr').find('th:last-child, td:last-child').remove()

Demo: Fiddle

Problem

I have an HTML TABLE: ``` <table id="persons" border="1"> <thead id="theadID"> <tr> <th>Name</th> <th>sex</th> <th>Message</th> </tr> </thead> <tbody id="tbodyID"> <tr> <td>Viktor</td> <td>Male</td> <td>etc</td> </tr> <tr> <td>Melissa</td> <td>Female</td> <td>etc</td> </tr> <tr> <td>Joe</td> <td>Male</td> <td>etc</td> </tr> </tbody> </table> <input type="button" onclick="deleteLastColumn();" value="do it"/> ``` I need a javascript/jquery code, which delete the last column (message) in the table: ``` function deleteLastColumn() { $("#theadID tr th:not(:last-child)...... $("#tbodyID tr td:not(:last-child)...... } ``` So the result should be this: ``` <table id="persons" border="1"> <thead id="theadID"> <tr> <th>Name</th> <th>sex</th> </tr> </thead> <tbody id="tbodyID"> <tr> <td>Viktor</td> <td>Male</td> </tr> <tr> <td>Melissa</td> <td>Female</td> </tr> <tr> <td>Joe</td> <td>Male</td> </tr> </tbody> </table> ``` I know there is the ":not(last)" method, but I can't find any example to my problem. Could anyone help me?

Original source