• We just launched and are currently in beta. Join us as we build and grow the community.

Combine Two Table Using JavaScript

marky2321

Mood Uplifter
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
37
Likes
119
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. <body

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    Combine Two Table Using JavaScript</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-6"

    >
  18. <h3

    >

    Person 1</

    h3

    >
  19. <table

    class

    =

    "table table-bordered"

    >
  20. <thead

    >
  21. <tr

    >
  22. <th

    >

    Name</

    th

    >
  23. </

    tr

    >
  24. </

    thead

    >
  25. <tbody

    id

    =

    "result1"

    ></

    tbody

    >
  26. </

    table

    >
  27. <br

    /

    >
  28. <h3

    >

    Person 2</

    h3

    >
  29. <table

    class

    =

    "table table-bordered"

    >
  30. <thead

    >
  31. <tr

    >
  32. <th

    >

    Name</

    th

    >
  33. </

    tr

    >
  34. </

    thead

    >
  35. <tbody

    id

    =

    "result2"

    ></

    tbody

    >
  36. </

    table

    >
  37. </

    div

    >
  38. <div

    class

    =

    "col-md-6"

    >
  39. <button

    class

    =

    "btn btn-primary"

    id

    =

    "combine"

    >

    Combine</

    button

    >
  40. <br

    /

    ><br

    /

    >
  41. <table

    class

    =

    "table table-bordered"

    >
  42. <thead

    >
  43. <tr

    >
  44. <th

    >

    Name</

    th

    >
  45. </

    tr

    >
  46. </

    thead

    >
  47. <tbody

    id

    =

    "display"

    ></

    tbody

    >
  48. </

    table

    >
  49. </

    div

    >

  50. </

    div

    >
  51. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  52. </

    body

    >
  53. </

    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. initiateArray(

    person1,

    person2)

    ;

  4. function

    initiateArray(

    array1,

    array2)

    {
  5. var

    html1=

    ""

    ;
  6. var

    html2=

    ""

    ;
  7. for

    (

    var

    i=

    0

    ;

    i<

    array1.length

    ;

    i++

    )

    {
  8. html1+=

    "<tr>"

    ;
  9. html1+=

    "<td>"

    +

    array1[

    i]

    +

    "</td>"

    ;
  10. html1+=

    "</tr>"

    ;
  11. }

  12. for

    (

    var

    i=

    0

    ;

    i<

    array2.length

    ;

    i++

    )

    {
  13. html2+=

    "<tr>"

    ;
  14. html2+=

    "<td>"

    +

    array2[

    i]

    +

    "</td>"

    ;
  15. html2+=

    "</tr>"

    ;
  16. }

  17. document.getElementById

    (

    'result1'

    )

    .innerHTML

    =

    html1;
  18. document.getElementById

    (

    'result2'

    )

    .innerHTML

    =

    html2;
  19. }

  20. document.getElementById

    (

    'combine'

    )

    .addEventListener

    (

    'click'

    ,

    combineTable.bind

    (

    this

    ,

    person1,

    person2)

    )

    ;

  21. function

    combineTable(

    array1,

    array2)

    {
  22. var

    combine =

    array1.concat

    (

    array2)

    ;
  23. var

    html =

    ""

    ;
  24. for

    (

    var

    i=

    0

    ;

    i<

    combine.length

    ;

    i++

    )

    {
  25. html+=

    "<tr>"

    ;
  26. html+=

    "<td>"

    +

    combine[

    i]

    +

    "</td>"

    ;
  27. html+=

    "</tr>"

    ;
  28. }

  29. document.getElementById

    (

    'display'

    )

    .innerHTML

    =

    html;
  30. }

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!

 

452,292

323,341

323,350

Top