Combine Two Table Using JavaScript

In this tutorial we will try to do Combine Two Table using JavaScript. The program will combine the two table by merging the two array object. The trick of this code is to use dot notation concat() in order to merge the two arrays. To learn more about this, just follow the steps below.

Getting started:

First you have to download bootstrap framework, 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 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">Combine Two Table Using JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;" />
  16. <div class="col-md-6">
  17. <h3>Person 1</h3>
  18. <table class="table table-bordered">
  19. <tr>
  20. <th>Name</th>
  21. </tr>
  22. </thead>
  23. <tbody id="result1"></tbody>
  24. </table>
  25. <br />
  26. <h3>Person 2</h3>
  27. <table class="table table-bordered">
  28. <tr>
  29. <th>Name</th>
  30. </tr>
  31. </thead>
  32. <tbody id="result2"></tbody>
  33. </table>
  34. </div>
  35. <div class="col-md-6">
  36. <button class="btn btn-primary" id="combine">Combine</button>
  37. <br /><br />
  38. <table class="table table-bordered">
  39. <tr>
  40. <th>Name</th>
  41. </tr>
  42. </thead>
  43. <tbody id="display"></tbody>
  44. </table>
  45. </div>
  46.  
  47. </div>
  48. <script src="js/script.js"></script>
  49. </body>
  50. </html>

Creating the Script

This code contains the script of the application. The code will dynamically combine two table when user click the button. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var person1 = ["John Smith"];
  2. var person2 = ["Claire Temple"];
  3.  
  4. initiateArray(person1, person2);
  5.  
  6. function initiateArray(array1, array2){
  7. var html1="";
  8. var html2="";
  9. for(var i=0; i<array1.length; i++){
  10. html1+="<tr>";
  11. html1+="<td>"+array1[i]+"</td>";
  12. html1+="</tr>";
  13. }
  14.  
  15. for(var i=0; i<array2.length; i++){
  16. html2+="<tr>";
  17. html2+="<td>"+array2[i]+"</td>";
  18. html2+="</tr>";
  19. }
  20.  
  21. document.getElementById('result1').innerHTML=html1;
  22. document.getElementById('result2').innerHTML=html2;
  23. }
  24.  
  25. document.getElementById('combine').addEventListener('click', combineTable.bind(this, person1, person2));
  26.  
  27. function combineTable(array1, array2){
  28. var combine = array1.concat(array2);
  29. var html ="";
  30. for(var i=0; i<combine.length; i++){
  31. html+="<tr>";
  32. html+="<td>"+combine[i]+"</td>";
  33. html+="</tr>";
  34. }
  35.  
  36. document.getElementById('display').innerHTML=html;
  37. }
There you have it we successfully created a Combine Two 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!

Add new comment