Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery remove HTML table column

Tags:

I have a HTML table like this:

<table border="1">     <tbody>         <tr>             <td><a href="#" class="delete">DELETE ROW</a>COL 1</td>             <td><a href="#" class="delete">DELETE COL</a>COL 2</td>             <td><a href="#" class="delete">DELETE COL</a>COL 3</td>             <td><a href="#" class="delete">DELETE COL</a>COL 4</td>             <td><a href="#" class="delete">DELETE COL</a>COL 5</td>             <td><a href="#" class="delete">DELETE COL</a>COL 6</td>         </tr>         <tr>             <td>ROW 1</td>             <td>ROW 1</td>             <td>ROW 1</td>             <td>ROW 1</td>             <td>ROW 1</td>             <td>ROW 1</td>         </tr>         <tr>             <td>ROW 2</td>             <td>ROW 2</td>             <td>ROW 2</td>             <td>ROW 2</td>             <td>ROW 2</td>             <td>ROW 2</td>         </tr>     </tbody> </table> 

I need a function to remove the specified column when I click on the link with the class "delete". Can you help ?

like image 736
Manny Calavera Avatar asked Jul 01 '09 12:07

Manny Calavera


People also ask

How to remove html table column?

Use str.search(“someColumnName”) (Because, we want to remove the column with some columnName) to match the current column name and the column name that we want to remove. If the column name matches then delete its every cell by .

How can delete column in jQuery?

To remove a column, it is not ideal to assign a delete button like we did for removing rows. Instead, the column will be removed when the column header is clicked. In the jQuery code, we need to attach a click event to the table header.

How to remove data from html table using jQuery?

The jQuery remove() method is used to remove a row from HTML table. jQuery remove() Method: This method removes the selected elements alongwith text and child nodes. This method also removes data and events of the selected elements. Parameters: It accepts single parameter selector which is optional.


1 Answers

After a few years, it's probably time to update the answer on this question.

// Listen for clicks on table originating from .delete element(s) $("table").on("click", ".delete", function ( event ) {     // Get index of parent TD among its siblings (add one for nth-child)     var ndx = $(this).parent().index() + 1;     // Find all TD elements with the same index     $("td", event.delegateTarget).remove(":nth-child(" + ndx + ")"); }); 
like image 86
Sampson Avatar answered Oct 31 '22 22:10

Sampson