How to Add New Row in HTML Table using JavaScript

How to Add New Row in HTML Table using JavaScript

Introduction

In this tutorial we will create a How to Add New Row in HTML Table using JavaScript. This tutorial purpose is to teach you on how to make add new row in html table dynamically. This will cover all the important functionality that will add row for your data 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 if you want to add new table row. I will give my best to provide you the easiest way of creating this program Adding new Table Row. So let's do the coding.

Before we get started:

Here 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 only display the html form and table with data. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE hmtl>
  2. <html lang="en">
  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. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Add New Row in HTML Table</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-8">
  17. <table class="table table-bordered">
  18. <thead class="alert-info">
  19. <tr>
  20. <th>Firstname</th>
  21. <th>Lastname</th>
  22. </tr>
  23. </thead>
  24. <tbody id="result">
  25. <tr><td>Claire</td><td>Tenple</td></tr>
  26. </tbody>
  27. </table>
  28. </div>
  29. <div class="col-md-4">
  30. <form>
  31. <div class="form-group">
  32. <label>Firstname</label>
  33. <input type="text" id="firstname" class="form-control" />
  34. </div>
  35. <div class="form-group">
  36. <label>Lastname</label>
  37. <input type="text" id="lastname" class="form-control" />
  38. </div>
  39. <center><button type="button" class="btn btn-primary" onclick="addNewRow();">ADD</button></center>
  40. </form>
  41. </div>
  42. </div>
  43. <script src="script.js"></script>
  44. </body>
  45. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will add new Table row when you submit the data inputs. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function addNewRow(){
  2. let firstname=document.getElementById('firstname').value;
  3. let lastname=document.getElementById('lastname').value;
  4.  
  5. if(firstname =="" || lastname ==""){
  6. alert("Please enter something first!");
  7. }else{
  8. let parent=document.createElement('tr');
  9. let col1=document.createElement('td');
  10. let col2=document.createElement('td');
  11. let text1=document.createTextNode(firstname);
  12. let text2=document.createTextNode(lastname);
  13. col1.appendChild(text1);
  14. col2.appendChild(text2);
  15. parent.appendChild(col1);
  16. parent.appendChild(col2);
  17.  
  18. document.getElementById('result').appendChild(parent);
  19.  
  20. document.getElementById('firstname').value="";
  21. document.getElementById('lastname').value="";
  22. }
  23.  
  24. }

This code above uses only one method to perform the function for adding new row. We will call this method addNewRow(), this function will dynamically add new table row after submitting the data inputs. This method include of create new text element and appending the data into the html table .

Output:

The How to Add New Row in HTML Table using 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 Add New Row in HTML Table 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment