1

I have this element

<tr class="group-row">
  <td>1</td>
  <td>2</td>
  <td>3</td>
</tr>

I want to clone it and add <tbody> parent to it to be like this

<tbody>
  <tr class="group-row">
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
</tbody>
  • If you have a `table` element, then a `tbody` will already exist within it. Even if it's not set in your HTML, the browser renderer will add one as it's required to keep the HTML valid. Example of this here: https://jsfiddle.net/RoryMcCrossan/9n1py843/. Inspect the DOM in the output to see that the `tbody` has been dynamically created for you. – Rory McCrossan Aug 03 '22 at 10:36
  • i can agree with above, it might be helpfull to know **why** u would want to do this or what the initial problem is – john Smith Aug 03 '22 at 10:39
  • @RoryMcCrossan I know that, but I need to add this parent for a particular case – Yousef Salama Aug 03 '22 at 10:40
  • https://api.jquery.com/clone & https://api.jquery.com/wrap – Rory McCrossan Aug 03 '22 at 10:41
  • Does this answer your question? [How do I wrap an element that has been cloned and appended?](https://stackoverflow.com/questions/29721918/how-do-i-wrap-an-element-that-has-been-cloned-and-appended) – Heretic Monkey Aug 03 '22 at 11:15

1 Answers1

2

something like this should work :

var $clone = $("#element-to-clone").clone();
$("#where-you-want-to-put-the-clone").append($clone);
$clone.wrap( "<tbody></tbody>" );