I have this table:
<table id="test">
<thead>
<tr><td>Main Head 1</td><td>Main Head 2</td></tr>
<tr><td>Sub Head 1</td><td>Sub Head 2</td></tr>
</thead>
<tbody>
<tr><td>Data 1</td><td>Data 2</td></tr>
<tr><td>Data 1</td><td>Data 2</td></tr>
<tr><td>Data 1</td><td>Data 2</td></tr>
</tbody>
</table>
I like to insert a new first row in the thead
so it looks like:
<table>
<thead>
<tr><td colspan="2">HEADING</td>/tr>
<tr><td>Main Head 1</td><td>Main Head 2</td></tr>
<tr><td>Sub Head 1</td><td>Sub Head 2</td></tr>
</thead>
<tbody>
<tr><td>Data 1</td><td>Data 2</td></tr>
<tr><td>Data 1</td><td>Data 2</td></tr>
<tr><td>Data 1</td><td>Data 2</td></tr>
</tbody>
</table>
I am using jQuery, I tried:
var cln = $('#test').clone();
cln.prepend('<tr><th>HEADING</th></tr>');
But it puts it in the first row of the body.
2
Answers
You’re trying to insert your element above the thead, use child selector to insert inside the thead.
Using
find()
, findthead
inside your cloned element, and useprepend
whatever you want to prepend.Example: