jQuery Remove Column From Table Examples
Ever wanted to remove column from table or remove multiple columns from a table using jQuery? In this article, I am going to show you with examples, how easy it is to remove / delete table column using jQuery.
How to Remove Single Column or Multiple Columns From A Table using jQuery
Following is the HTML source code I used for the examples:
HTML Source:
<html> <head> <title>Remove Table Columns</title> <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script> </head> <body> <table width="100%" border="0" cellspacing="0" cellpadding="0" id="my_table"> <thead> <tr> <th>H1</th> <th>H2</th> <th>H3</th> <th>H4</th> </tr> </thead> <tr> <td>1</td> <td>6</td> <td>11</td> <td>16</td> </tr> <tr> <td>2</td> <td>7</td> <td>12</td> <td>17</td> </tr> <tr> <td>3</td> <td>8</td> <td>13</td> <td>18</td> </tr> <tr> <td>4</td> <td>9</td> <td>14</td> <td>19</td> </tr> <tr> <td>5</td> <td>10</td> <td>15</td> <td>20</td> </tr> </table> <button id="remove_column">Remove Column</button> </body> </html>
In this example, we are going to remove table columns when the “Remove Column” button is clicked. So all code will go into the click event of the button. With that in mind, let’s take a look at how you can remove a single column and / or remove multiple columns from table using jQuery. The acutal code will be following by additional code that you can use to remove the headers as well, if they exist in your table.
Example 1 (a): Remove all Columns except first column of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_column").click( function() {
//Remove all columns except first column
$('#my_table td:not(:nth-child(1))').remove();
//Remove all columns except first column along with Header
$('#my_table th:not(:nth-child(1)), #my_table td:not(:nth-child(1))').remove();
});
});
</script>Example 1 (b): Remove all Columns except first column of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_column").click( function() {
//Alternate way to Remove all columns except first column
$("#my_table").find("td:not(:nth-child(1))").remove();
//Alternate way to Remove all columns except first column along with Header
$("#my_table").find("th:not(:nth-child(1)), td:not(:nth-child(1))").remove();
});
});
</script>Example 2: Remove only second column of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_column").click( function() {
//Remove only 2nd column
$('#my_table td:nth-child(2)').remove();
//Remove only 2nd column along with Header
$('#my_table th:nth-child(2), #my_table td:nth-child(2)').remove();
});
});
</script>Example 3: Remove only third column of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_column").click( function() {
//Remove only 3rd column
$('#my_table td:nth-child(3)').remove();
//Remove only 3rd column along with Header
$('#my_table th:nth-child(3), #my_table td:nth-child(3)').remove();
});
});
</script>Example 4: Remove all Columns except second column of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_column").click( function() {
//Remove all columns except second
$('#my_table td:not(:nth-child(2))').remove();
//Remove all columns except second along with Header
$('#my_table th:not(:nth-child(2)), #my_table td:not(:nth-child(2))').remove();
});
});
</script>Example 5: Remove last column of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_column").click( function() {
//Remove last column
$('#my_table td:last-child').remove();
//Remove last column along with Header
$('#my_table th:last-child, #my_table td:last-child').remove();
});
});
</script>Example 6: Remove all Columns except last column of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_column").click( function() {
//Remove all columns except last column
$('#my_table td:not(:last-child)').remove();
//Remove all columns except last column along with Header
$('#my_table th:not(:last-child), #my_table td:not(:last-child)').remove();
});
});
</script>As you see, all it takes is just 1 line of code to remove the column that you desire.
Your Turn!
Do you know of any other ways to remove table column or remove columns from table using jQuery? Share your answers by commenting below.