Can an element created in jquery create a brother?

0

I have created an element in jquery and I have stored it in a variable as I show it below:

x = $("<tr><td> Texto </td></tr>");

and if I print it in console in this way console.log (x [0]) print me the following

<tr><td> Texto </td></tr>

My question is whether there is a function or a way for the variable x to create a sibling and that when I print on the console I print something similar to the following

<tr><td> Un hermano antes </td></tr> 
<tr><td> Texto </td></tr>
<tr><td> Un hermano despues </td></tr>
    
asked by Epifanio Pérez Ramírez 12.11.2018 в 22:34
source

1 answer

0

You can use clone() to clone the element, and then before() and after() to add them before or after it.

$(document).ready(function(){
  let $orig = $("#original");
  let $clon = $orig.clone();
  $clon.text("Soy el clon 1");
  $orig.before($clon);
  $clon = $orig.clone();
  $clon.text("Soy el clon 2");
  $orig.after($clon);
   

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id="original"><td >Soy el original</td></tr>
<table>
    
answered by 12.11.2018 в 22:44