How to Dynamically Remove Table Row in JavaScript

How to Dynamically Remove Table Row in JavaScript

Introduction

In this tutorial we will create a How to Dynamically Remove Table Row in JavaScript. This tutorial purpose is to teach you how you can remove table row. This will cover all the basic function that will remove a row from the table. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that use table to display your data. I will give my best to provide you the easiest way of creating this program Remove row from table. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display the html table and buttons. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  5. </head>
  6. <nav class="navbar navbar-default">
  7. <div class="container-fluid">
  8. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  9. </div>
  10. </nav>
  11. <div class="col-md-3"></div>
  12. <div class="col-md-6 well">
  13. <h3 class="text-primary">How to Dynamically Remove Table Row</h3>
  14. <hr style="border-top:1px dotted #ccc;"/>
  15. <table id="table" class="table table-bordered">
  16. <thead class="alert-info">
  17. <tr>
  18. <th>Firstname</th>
  19. <th>Lastname</th>
  20. <th>Address</th>
  21. <th>Gender</th>
  22. <th>Age</th>
  23. <th>Action</th>
  24. </tr>
  25. </thead>
  26. <tr>
  27. <td>John</td>
  28. <td>Smith</td>
  29. <td>USA</td>
  30. <td>Male</td>
  31. <td>26</td>
  32. <td><button class="btn btn-danger" onclick="removeRow(this)">Remove</td>
  33. </tr>
  34. <tr>
  35. <td>Claire</td>
  36. <td>Temple</td>
  37. <td>USA</td>
  38. <td>Female</td>
  39. <td>21</td>
  40. <td><button class="btn btn-danger" onclick="removeRow(this)">Remove</td>
  41. </tr>
  42. <tr>
  43. <td>Tony</td>
  44. <td>Rogers</td>
  45. <td>New York</td>
  46. <td>Male</td>
  47. <td>35</td>
  48. <td><button class="btn btn-danger" onclick="removeRow(this)">Remove</td>
  49. </tr>
  50. <tr>
  51. <td>Bobby</td>
  52. <td>Roberson</td>
  53. <td>Italy</td>
  54. <td>Male</td>
  55. <td>31</td>
  56. <td><button class="btn btn-danger" onclick="removeRow(this)">Remove</td>
  57. </tr>
  58. </tbody>
  59. </table>
  60. </div>
  61. <script src="script.js"></script>
  62.  
  63. </body>
  64. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically remove a row from table when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function removeRow(row) {
  2. let i = row.parentNode.parentNode.rowIndex;
  3. document.getElementById("table").deleteRow(i);
  4. }

In the code above we will just create a method called removeRow(). This function will dynamically remove the row of the button you just have clicked. Inside the function we first create a variable that will hold the index position of the row. Lastly we will target our table row and use the deleteRow() function

Output:

The How to Dynamically Remove Table Row in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Dynamically Remove Table Row in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment