marky2321
Mood Uplifter
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.
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.
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"
>
- <a
class
=
"navbar-brand"
href
=
"https://sourcecodester.com"
>
Sourcecodester</
a
>
- </
div
>
- </
nav>
- <div
class
=
"col-md-3"
></
div
>
- <div
class
=
"col-md-6 well"
>
- <h3
class
=
"text-primary"
>
Combine Two Table Using JavaScript</
h3
>
- <hr
style
=
"border-top:1px dotted #ccc;"
/
>
- <div
class
=
"col-md-6"
>
- <h3
>
Person 1</
h3
>
- <table
class
=
"table table-bordered"
>
- <thead
>
- <tr
>
- <th
>
Name</
th
>
- </
tr
>
- </
thead
>
- <tbody
id
=
"result1"
></
tbody
>
- </
table
>
- <br
/
>
- <h3
>
Person 2</
h3
>
- <table
class
=
"table table-bordered"
>
- <thead
>
- <tr
>
- <th
>
Name</
th
>
- </
tr
>
- </
thead
>
- <tbody
id
=
"result2"
></
tbody
>
- </
table
>
- </
div
>
- <div
class
=
"col-md-6"
>
- <button
class
=
"btn btn-primary"
id
=
"combine"
>
Combine</
button
>
- <br
/
><br
/
>
- <table
class
=
"table table-bordered"
>
- <thead
>
- <tr
>
- <th
>
Name</
th
>
- </
tr
>
- </
thead
>
- <tbody
id
=
"display"
></
tbody
>
- </
table
>
- </
div
>
- </
div
>
- <script
src
=
"js/script.js"
></
script
>
- </
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;
- }
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!