Combine Two Table Using JavaScript
Submitted by razormist on Thursday, June 11, 2020 - 08:57.
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.
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;" />
- <div class="col-md-6">
- <table class="table table-bordered">
- <thead>
- <tr>
- </tr>
- </thead>
- </table>
- <br />
- <table class="table table-bordered">
- <thead>
- <tr>
- </tr>
- </thead>
- </table>
- </div>
- <div class="col-md-6">
- <table class="table table-bordered">
- <thead>
- <tr>
- </tr>
- </thead>
- </table>
- </div>
- </div>
- </body>
- </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.- var person1 = ["John Smith"];
- var person2 = ["Claire Temple"];
- initiateArray(person1, person2);
- function initiateArray(array1, array2){
- var html1="";
- var html2="";
- for(var i=0; i<array1.length; i++){
- html1+="<tr>";
- html1+="<td>"+array1[i]+"</td>";
- html1+="</tr>";
- }
- for(var i=0; i<array2.length; i++){
- html2+="<tr>";
- html2+="<td>"+array2[i]+"</td>";
- html2+="</tr>";
- }
- document.getElementById('result1').innerHTML=html1;
- document.getElementById('result2').innerHTML=html2;
- }
- document.getElementById('combine').addEventListener('click', combineTable.bind(this, person1, person2));
- function combineTable(array1, array2){
- var combine = array1.concat(array2);
- var html ="";
- for(var i=0; i<combine.length; i++){
- html+="<tr>";
- html+="<td>"+combine[i]+"</td>";
- html+="</tr>";
- }
- document.getElementById('display').innerHTML=html;
- }
Add new comment
- 431 views