Download Source Code -JavaScript – Dynamically Remove Table Row

Loading...

JavaScript – Dynamically Remove Table Row

Submitted by: 
razormist
Language: 
JavaScript
Visitors have accessed this post 5 times.

In this tutorial we will create a Dynamically Remove Table Row using JavaScript. This code will dynamically remove a html table row when the user click the remove button. The code use onclick() function in order to initiate a certain function that can remove a table row using parentNode.parentNode.rowIndex by applying the index of the clicked element before the dot notation. You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it.

We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

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

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">JavaScript - Dynamically Remove Table Row</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-1"></div>
  19. <div class="col-md-10">
  20. <table id="table" class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Firstname</th>
  24. <th>Lastname</th>
  25. <th>Address</th>
  26. <th>Gender</th>
  27. <th>Action</th>
  28. </tr>
  29. </thead>
  30. <tbody>
  31. <tr>
  32. <td>John</td>
  33. <td>Smith</td>
  34. <td>USA</td>
  35. <td>Male</td>
  36. <td><button class="btn btn-danger" onclick="deleteRow(this)">Remove</td>
  37. </tr>
  38. <tr>
  39. <td>Claire</td>
  40. <td>Temple</td>
  41. <td>USA</td>
  42. <td>Female</td>
  43. <td><button class="btn btn-danger" onclick="deleteRow(this)">Remove</td>
  44. </tr>
  45. <tr>
  46. <td>Tony</td>
  47. <td>Rogers</td>
  48. <td>New York</td>
  49. <td>Male</td>
  50. <td><button class="btn btn-danger" onclick="deleteRow(this)">Remove</td>
  51. </tr>
  52. </tbody>
  53. </table>
  54. </div>
  55. </div>
  56. <script src="js/script.js"></script>
  57.  
  58. </body>
  59. </html>

Creating the Script

This code contains the script of the application. This code will remove a html table row when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory

  1. function deleteRow(row) {
  2. var index = row.parentNode.parentNode.rowIndex;
  3. document.getElementById("table").deleteRow(index);
  4. }

There you have it we successfully created a Dynamically Remove Table Row using 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!

Tags: 
Javascript
table
Delete
Bootstrap

Add new comment

(Visited 2 times, 1 visits today)
Loading...