I have two separate table, I want to merge into a single table using JQuery. But the out put I am seeing is not the correct one. Any help regarding this will be appreciated.
Source Table
<html>
<table class="left-column">
<tbody>
<tr>
<td>Name
</td>
</tr>
<tr>
<td>Age
</td>
</tr>
</tbody>
</table>
<table class="right-column">
<tbody>
<tr>
<td>Andy
</td>
</tr>
<tr>
<td>30
</td>
</tr>
</tbody>
</table>
</html>
Target Table
<html>
<table>
<tr>
<td>
<table class="left-column">
<tbody>
<tr>
<td>Name
</td>
</tr>
<tr>
<td>Age
</td>
</tr>
</tbody>
</table>
</td>
<td>
<table class="right-column">
<tbody>
<tr>
<td>Andy
</td>
</tr>
<tr>
<td>30
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</html>
Jquery Code snippet
var leftTable=$('table.left-column').clone();
var rightTable=$('table.right-column').clone();
var newTable= $('div').append('<table><tr><td>')
.append(leftTable).append('</td><td>')
.append(rightTable).append('</td></tr></table>')
.html();
return newTable;
How about something like this (jsFiddle). This gives exactly the output you’ve requested in 3 short lines of code. (NOTE, this code actually MOVES the individual tables into the joined table instead of copying/cloning them.